Last chance to get discounted tickets to Codegarden... Price goes up on Tuesday at 20:00 CEST!

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 254 posts 873 karma points
    1 week ago
    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 2590 posts 7569 karma points MVP 2x admin c-trib
    7 days ago
    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 254 posts 873 karma points
    7 days ago
    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...

Please Sign in or register to post replies

Write your reply to:

Draft