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 280 posts 974 karma points MVP c-trib
    Apr 15, 2018 @ 11:11
    Nathan Woulfe

    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 2798 posts 8563 karma points MVP 3x admin c-trib
    Apr 16, 2018 @ 06:27
    Dave Woestenborghs

    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


  • Nathan Woulfe 280 posts 974 karma points MVP c-trib
    Apr 16, 2018 @ 07:07
    Nathan Woulfe

    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...

Please Sign in or register to post replies

Write your reply to: