namespace customer.UnitTest { [TestClass] publicclassTestUsers { [TestMethod] publicvoid GetUser_ShouldReturnMichael() {
var testValue = "Michael"; var controller =new customer.Controllers.UserUsersApiController(); var result = controller.GetUser(); Assert.AreEqual(testValue, result); } } }
When I run this very simple test I get the following error:
Result Message: Test method customer.UnitTest.TestUsers.GetUser_ShouldReturnMichael threw exception: System.ArgumentNullException: Value cannot be null. Parameter name: umbracoContext Result StackTrace: at Umbraco.Web.WebApi.UmbracoApiController..ctor(UmbracoContext umbracoContext) at Umbraco.Web.WebApi.UmbracoApiController..ctor() at customer.Controllers.UsersApiController..ctor() at customer.UnitTest.TestUsers. GetUser_ShouldReturnMichael() in xxx
It is the line:
var controller =new customer.Controllers.UserUsersApiController();
that triggers the error but I can't seem to find out why.
Looks like the UmbracoContext is empty. I know that Umbraco also have some unit tests in the source code. Maybe check those out to see if they have any UmbracoApiController tests.
I actually kind of gave up on testing controllers that is using the context from Umbraco. I'm trying to seperate my code as much as possible so I'll be able to test the most important parts of it.
I am trying to do the very same thing. I added the Umbraco.Tests.dll to the test project, inherited my test class from BaseRoutingTest and set the UmbracoContext by calling GetRoutingContext("/test").UmbracoContext. Still, I could not get it working as it complains about some missing config files. Any ideas?
Unit testing a web api controller
I'm trying to setup a unit test of this web api controller.
My very simple unit test looks like this:
When I run this very simple test I get the following error:
It is the line:
that triggers the error but I can't seem to find out why.
Am I missing something?
Looks like the UmbracoContext is empty. I know that Umbraco also have some unit tests in the source code. Maybe check those out to see if they have any UmbracoApiController tests.
Jeroen
Hey Jeroen.
Thanks for leading me in the right direction.
There's actually a great article on this issue with a description on how to obtain an UmbracoContext when unit testing.
https://groups.google.com/forum/?fromgroups=#!topic/umbraco-dev/vEjdzjqmtsU
//Michael
Hi Michael,
How did you eventually end up testing your controllers?
I have tried adding a constructor to my controllers which accepts an
UmbracoContext
but I'm not sure how to mock this object.Grtz
L
I actually kind of gave up on testing controllers that is using the context from Umbraco. I'm trying to seperate my code as much as possible so I'll be able to test the most important parts of it.
/Michael
Hmmm I can't seem to catch a break either, testing code seperately is fair enough for now.
Adding a constructor is not a good idea btw, that seems to break the service when running within Umbraco.
Thanks for quick reply :)
I am trying to do the very same thing. I added the Umbraco.Tests.dll to the test project, inherited my test class from BaseRoutingTest and set the UmbracoContext by calling GetRoutingContext("/test").UmbracoContext. Still, I could not get it working as it complains about some missing config files. Any ideas?
is working on a reply...