Copied to clipboard

Flag this post as spam?

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


  • Nathan Woulfe 422 posts 1580 karma points MVP 3x c-trib
    Apr 15, 2018 @ 11:11
    Nathan Woulfe
    0

    Mocking ContentType on IPublishedContent

    Having some difficulty mocking a content type onto a mocked IPublishedContent. Seems there's no interface to PublishedContentType, and the constructor is protected/not accessible.

            Mock<IPublishedContent> child = mock.Create<IPublishedContent>();
            child.Setup(x => x.Id).Returns(1099);
            child.Setup(x => x.Level).Returns(2);
            child.Setup(x =>x.ContentType).Returns(A MOCKED CONTENT TYPE);
    

    Ignoring the last line, that works fine to mock out the content.

    Is it possible to mock the type as well? Should I just live with 99% test coverage because I can't hit the statement that needs to check the content type ID?

    I've taken an alternate path and actually scaffolded out a content tree, populated with real content, but that then has it's own issues where the IPublishedContent versions aren't available due to the cache resolver not being initialized (or something like that).

    Any suggestions would be awesome.

    P.S. testing is fun.

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Apr 16, 2018 @ 06:27
    Dave Woestenborghs
    0

    Hi Nathan,

    I think directly mocking is not possible because the ContentType is a concrete class.

    I use Unit testing adapter from Lars-Erik and that has method to "mock" it

    https://github.com/lars-erik/umbraco-unit-testing-samples/blob/master/Umbraco.UnitTesting.Adapter/Support/UmbracoSupport.cs#L200

    Dave

  • Nathan Woulfe 422 posts 1580 karma points MVP 3x c-trib
    Apr 16, 2018 @ 07:07
    Nathan Woulfe
    0

    Nice. Thought I'd read every blog post ever written about testing Umbraco.

    I'm in a strange purgatory where I'm wanting to do integration and unit testing, probably more of the former than the latter. So I need a functioning database (which I have), but also need a full Umbraco context, which I kind of have.

    Will keep plugging away...

  • 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