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...
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.
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:
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.
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:
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
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.
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
Really happy the post helped. :)
The trick is to know when to abstract and when to just deliver.
Best of luck!
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.
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:
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. :)
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 :-)
Cool! :)
is working on a reply...