Surely you'll get less coverage than unit-testing, but you can get a reasonable coverage which cost way less time doing unit-testing and is more flexible. That is, if you unit-test everything you have to update one or more tests each time you modify a simple element. Because without knowing the context you can't say for sure if you need to test this or that.
Here are some reasons, why you might want to test controllers:. A unit test for a controller is pointless. You test your controllers implicitly via regression tests using Selenium for example. Then everything is properly unit tested. What you want to be certain of is that everything works well together when performing web requests.
That's regression testing. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Learn more. Why would you write unit-tests for controllers? Ask Question. Asked 4 years, 10 months ago. Active 1 year, 10 months ago. Viewed 15k times. Content; Assert. IsEmpty content ; Assert. IsInstanceOf typeof System. ListOfConstituencies , Times. Improve this question. Possible duplicate of Where is the line between unit testing application logic and distrusting language constructs?
Don't agree with that gnat. This is not about language constructs necessarily but about the type of the result returned by a controller. While it may boil down to sort of the same thing when you have no inheritance hierarchy, it becomes an altogether different beast when the controller expects Ancestors to be returned, the controller returned Person and now Person is changed to descend not from Ancestor, but PeopleAncestor Also answers why testing such a method may be a good idea Tend to agree, I typically don't spend much time unit testing the entry points of the application.
It is like unit testing a main method of a console application. Role , then determine the next step based on the role's permission level. When unit testing an action method, we want to check the behaviors for different user roles, thus we need to simulate various users accessing the API endpoint. In the code snippet above, lines 1 to 5 stage a user with an Admin role.
Then lines 6 to 9 instantiate a controller instance and attach an HttpContext to the controller, where the HttpContext includes the staged user.
With that, line 11 executes the action method under test, and the rest are asserts. In the same way, we can arrange different kinds of claims and tie them to the controller for testing.
ToString ;. In order to stage a request header key-value pair, we can first attach an HttpContext to the controller, then set up the desired request header. The following code snippet shows an example unit test. Line 5 sets a key-value pair in the Request Headers collection. Note that line 3 is required because the Request object lives in HttpContext. The ModelState object lives in the controller, thus staging ModelState can be straightforward.
In the code above, lines 3 and 4 demonstrate how to alter the ModelState object. This code gives us the ability to verify validation logic in action methods. Again the source code can be found in my GitHub repository.
If someone tells me their controller requires unit testing, I will ask, did you design your controller properly? Unit testing is a mean for providing higher quality software and not an aim per se. Having too many redundant unit tests will cause a future maintenance headache and will cost more project development time.
Why do controllers get fat? I did blog earlier on how views become fat Three Steps to Get Fat-Razor MVC Views on Diet , some points are common and some are specific to controllers: Explicit validation of data in the action method Validation of your viewmodel data in your controller violates two principles: SRP Design Principle : You are attaching more responsibilities to the controller.
Encapsulation OOP Principle : You will have to expose your data outside your viewmodel to allow another object, the controller, to validate it, while the object itself should be responsible for its own data. Mapping a model to a viewmodel The mapping from a model to a viewmodel should not be done in the action method.
0コメント