In your case I think I'd look at the first one - move your method that does the calculating distance into its own class, that needs (as far as I can see) to have no reference to Umbraco, and write your tests around that. Then have your controller take an instance of that class as a dependency, or new one up in your controller's constructor or CalculateDistance method, and call out to your new class for the functionality.
In that case you would be unit testing the core function you want to test, but not testing the controller. However given the controller method remaining would be extremely simply, there's much less value in doing so.
Unit Testing
Hi,
I am trying to create my first unit tests. I have the following code:
The test fails and says the umbracoContext cannot be null. I found this post http://our.umbraco.org/forum/umbraco-7/using-umbraco-7/48015-Unit-Testing-Surface-Controllers saying I need the umbraco.test.dll but I cannot find it anywhere.
Hopefully someone can help me out.
You can finde the umbraco.test.dll on Github
https://github.com/umbraco/Umbraco-CMS
Just download and build the project
I added the .dll but still having trouble with the umbracoContext. The following code:
is null. How can I create a proper UmbracoContext?
I wrote a couple of blog posts on this problem recently. There's perhaps not an ideal solution, but some techniques that might help you:
http://web-matters.blogspot.it/2014/03/unit-testing-umbraco-surface-controllers.html
http://web-matters.blogspot.it/2014/04/unit-testing-umbraco-with-base-test-classes.html
In your case I think I'd look at the first one - move your method that does the calculating distance into its own class, that needs (as far as I can see) to have no reference to Umbraco, and write your tests around that. Then have your controller take an instance of that class as a dependency, or new one up in your controller's constructor or CalculateDistance method, and call out to your new class for the functionality.
In that case you would be unit testing the core function you want to test, but not testing the controller. However given the controller method remaining would be extremely simply, there's much less value in doing so.
Hope that helps.
Andy
is working on a reply...