ivaylokenov / AspNet.Mvc.TypedRouting

A collection of extension methods providing strongly typed routing and link generation for ASP.NET Core MVC projects.
https://mytestedasp.net/
MIT License
485 stars 36 forks source link

Unit Testing - way to run simple unit tests #15

Closed astian92 closed 8 years ago

astian92 commented 8 years ago

Hi Ivaylo,

Sorry if I am not writing this at the correct place for it. First of all, amazing work, thank you for providing us with a great way to end the reign of the magic strings. However, can I ask if it is possible to use this library in an action and test that action using a normal unit test. When I try to make a simple test that does not use a Test server it results in a NullReference exception. I saw that it is because it is using IServiceProvider and a some other stuff that are a part of the MVC architecture, while I am simply instantiating a Controller class and calling a method that returns a RedirectResult.

So, is it possible to test an action that uses it without running up a TestServer? (With just a simple UnitTest)

ivaylokenov commented 8 years ago

@astian92 Yes, it is possible without running a TestServer, although I admit it is not the most prettiest solution:

https://github.com/ivaylokenov/AspNet.Mvc.TypedRouting/blob/master/test/AspNet.Mvc.TypedRouting.Test/TestInit.cs

Just add all assemblies with controller types. If you run this code before the controller tests, then you can test controllers normally:

https://github.com/ivaylokenov/AspNet.Mvc.TypedRouting/blob/master/test/AspNet.Mvc.TypedRouting.Test/LinkGeneration/ControllerExtensionsTest.cs

Will this work for you? Because I have an idea where unit testing can be possible without all the setup.

astian92 commented 8 years ago

Hi,

Thanks. Using this code seems to do the trick. Thank you for your quick reaction and help. Good luck with this and your other wonderful libraries.