Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kevin T Reynolds 15 posts 158 karma points
    Apr 22, 2021 @ 18:57
    Kevin T Reynolds
    0

    Unit Testing Help

    I'm trying to introduce some unit testing to extended Umbraco functionality but I think I may be overcomplicating things.

    This is the Method I'm trying to test.

    public class CareersService : ICareersService
    {
        private readonly IPublishedContentQuery _contentQuery;
        private readonly IPublishedContentWrapper _publishedContent;
        private readonly IConverter<IPublishedContent, IJob> _converter;
    
        public CareersService(IPublishedContentQuery contentQuery, IPublishedContentWrapper publishedContent, IConverter<IPublishedContent, IJob> converter)
        {
            _contentQuery = contentQuery;
            _publishedContent = publishedContent;
            _converter = converter;
        }
    
        ...
    
        public IEnumerable<IJob> GetAllJobs()
        {
            var root = _contentQuery.ContentAtRoot().FirstOrDefault();
            var jobPostings = _publishedContent.DescendantsOfType(root, "jobPosting");
            var jobs = jobPostings.Select(x => _converter.Convert(x));
    
            return jobs;
        }
    
        ...
    }
    

    the IPublishedContentWrapper is a wrapper so I can test the DescendantsOfType extension method.

    public interface IPublishedContentWrapper
    {
        IEnumerable<IPublishedContent> DescendantsOfType(IPublishedContent node, string alias);
    }
    
    public class PublishedContentWrapper : IPublishedContentWrapper
    {
        public IEnumerable<IPublishedContent> DescendantsOfType(IPublishedContent node, string alias)
        {
            return node?.DescendantsOfType(alias) ?? Enumerable.Empty<IPublishedContent>();
        }
    }
    

    and the converter is just handling conversion to my job model

    public class IPublishedContentToIJobConverter : IConverter<IPublishedContent, IJob>
    {
        public IJob Convert(IPublishedContent source)
        {
            var results = new Job()
            {
                Id = source.Id,
                Name = source.Name,
                PostDate = source.Value<DateTime>("postDate"),
                JobTitle = source.Value<string>("jobTitle"),
                Url = source.Url(mode: UrlMode.Absolute),
                Active = source.Value<bool>("active")
            };
    
            return results;
        }
    }
    

    I was following along with the unit testing documentation so I'm using that base class with a couple of modifications and here's my actual test.

        [Fact]
        public void GetAllJobs_ReturnsEnumerableOfJobs()
        {
            var jobNode = new Mock<IPublishedContent>();
            jobNode.Setup(x => x.Id).Returns(782);
            jobNode.Setup(x => x.Name).Returns("Web Developer");
    
            var jobNodes = new List<IPublishedContent>()
            {
                jobNode.Object
            };
    
            var job = new Mock<IJob>();
            job.Setup(x => x.Id).Returns(782);
            job.Setup(x => x.Name).Returns("Web Developer");
    
            var jobs = new List<IJob>()
            {
                job.Object
            };
    
            var siteRoot = new Mock<IPublishedContent>();
            base.PublishedContentWrapper.Setup(x => x.DescendantsOfType(siteRoot.Object, "jobPosting")).Returns(jobNodes);
            _converter.Setup(x => x.Convert(jobNode.Object)).Returns(job.Object);
            var contentAtRoot = new List<IPublishedContent>()
            {
                siteRoot.Object
            };
            base.PublishedContentQuery.Setup(x => x.ContentAtRoot()).Returns(contentAtRoot);
    
            var result = _careers.GetAllJobs();
    
            Assert.Equal(jobs, result);
        }
    

    It passes but it feels unnecessarily complicated for such a simple method. Is there a way to simplify the test or a way you would restructure the method to be more testable?

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Apr 28, 2021 @ 11:23
    Dennis Adolfi
    101

    I think it looks good and not complicated at all. Your test is easy to follow along and if I would have been new to your project and read this test I would get a good understanding of what it does. #H5YR!

  • Hüseyin 13 posts 104 karma points
    Nov 20, 2023 @ 10:03
    Hüseyin
    0

    Hi there, I need to write some unit tests but we still got this error. I use these packages and this code. nothing else . umbraco v12.2.0 . xunit v2.5.3 . NSubstitute v5.1.0

    var content = Substitute.For<IPublishedContent>();
    content.Value<string>("title").Returns("Title");
    

    @DennisAdolfi

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies