Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 873 karma points
    Nov 27, 2016 @ 15:26
    jake williamson
    0

    it is possible to unit test a controller inheriting from UmbracoAuthorizedApiController?

    chaps, it is possible to unit test a back office controller inheriting from UmbracoAuthorizedApiController?

    at the moment, when i'm trying to mock my controller out i get:

    System.ArgumentNullException : Value cannot be null.
    Parameter name: umbracoContext at Umbraco.Web.WebApi.UmbracoApiControllerBase..ctor(UmbracoContext umbracoContext) at Umbraco.Web.WebApi.UmbracoAuthorizedApiController..ctor()
    

    i can move 99% of my code out into a separate service which would negate the need to test my controller but that feels like a work around...

    cheers,

    jake

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    Nov 28, 2016 @ 00:08
    Lars-Erik Aabech
    100

    You're stating the answer, but if you'd like to go the long way around, you can test most stuff in Umbraco that relies on an UmbracoContext. I've outlined the basic steps on my blog: http://blog.aabech.no/archive/the-basics-of-unit-testing-umbraco/.

    Most basic answer is: reference Umbraco.Tests from the core source. There's base classes there that handle the bulk of setting it up for you.

  • jake williamson 207 posts 873 karma points
    Nov 28, 2016 @ 19:25
    jake williamson
    0

    hey lars, i'm finding i go the long way round more and more these days ;)

    thanks for your reply, and for posting the link to your blog post (which i've read many times!).

    i'm pretty much there with it now!

    cheers,

    jake

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    Nov 28, 2016 @ 20:26
    Lars-Erik Aabech
    0

    Really happy the post helped. :)
    The trick is to know when to abstract and when to just deliver.
    Best of luck!

  • John Smith 6 posts 26 karma points
    Aug 07, 2018 @ 12:48
    John Smith
    0

    Hi Lars,

    Thanks for the post but I'm having problems trying to do an automated test on a plugin we've created. Basically I need to test a class which extends UmbracoAuthorizedApiController but the problem I'm facing is that UmbracoContext.Security.CurrentUser is always null, even if I adapt your UmbracoContext setup and set a User on the httpContextBase.

    I'm using Gherkin scripts with SpecFlow and I prefer to use xUnit as my test runner, so I can't use Umbraco.Tests.

    Any suggestions? Thanks.

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    Aug 07, 2018 @ 13:00
    Lars-Erik Aabech
    0

    Hi John,

    I don't have all the code in memory, so I hope you're fine with some general guidance. :)

    You'll have to investigate the Umbraco source so you know how it works. There are probably internals you can't get to in order to stub what you need. The benefit of using Umbraco.Tests is that it sets up most internals for you. If you're not using it, you'll have to manage to set those internals yourself.

    Which leaves two options:

    • Use reflection (Not recommended)
    • Create an assembly called "Umbraco.UnitTesting.Adapter" (Recommended)

    The latter has access to all internals due to an InternalsVisibleTo in core. It's supposed to be used from tests only, ofc. :)
    For older versions you can call it "Umbraco.VisualStudio".

    The sample repo from the blog post has been updated recently. You might find something similar to what you need here:

    https://github.com/lars-erik/umbraco-unit-testing-samples

    I also have some with SpecFlow, but I don't remember if I touched authentication:

    https://github.com/lars-erik/specflow-razorgenerator-umbraco-samples

    Hope this helps. :)

  • John Smith 6 posts 26 karma points
    Aug 07, 2018 @ 13:28
    John Smith
    0

    Hi Lars,

    It looks like your specflow examples have lots of good setup stuff in them. I'll wade through them and see if I can use some of the code from your examples.

    Thank you so much :-)

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    Aug 07, 2018 @ 13:32
    Lars-Erik Aabech
    0

    Cool! :)

Please Sign in or register to post replies

Write your reply to:

Draft