IGIT
IGIT

Reputation: 185

Is unit testing skinny controllers really necessary?

I am wondering if you guys do unit test your controllers and if it is common practise in a TDD environment.

What I have are skinny controllers that basically call on to the business layer for logic and data and then populate a local ViewModel to pass to the view.

I do unit test the business layer but I wonder if testing these skinny controllers still make sense or its really not necessary unless you have a fat controller with a lot of logic in it.

Please let me know your thoughts.

Thanks.

Upvotes: 2

Views: 264

Answers (3)

guillaume31
guillaume31

Reputation: 14080

As a rule of thumb, I don't bother testing things that don't have any logic. Just calling another object hardly counts as logic IMO. Calling multiple other objects in a sequence where order matters is more debatable.

What I would do if I use an outside-in approach is TDD the controller by stubbing out its dependencies (business services, mappers, etc.) before I go one step further up the food chain and implement the real dependencies. But I rarely explicitly assert that dependencies should be called in a particular order. At the end of the day, these tests are more about design than proving correctness, and you could reasonably delete them once the whole graph of classes has been written.

Upvotes: 1

Old Fox
Old Fox

Reputation: 8725

Creates a UT for class which all his doing is to forward calls from one point to another is not a good idea. The reason is quite simple: it's just not worth it.... you won't receive any benefit from those tests, more over you'll have to maintain those test over the time while you won't verify any behavior(just stracture...)

So how do I cover those units? the way is also simple; just test them as a part of something which use them.

Now back to the original question... The way I found to test my skinny controllers is through a component/integration tests. I start from the BL and when I have to coupled the BL with the controller I create a component/integration tests and then end the task with the controller implementation.

Upvotes: 4

Big McLargeHuge
Big McLargeHuge

Reputation: 16076

Personally, no. But I don't normally do TDD either.

If you did write your tests first, you might as well leave them. They're not hurting anything.

If you didn't write your tests first, I agree that it doesn't make sense to add them afterwards, because at that point you're basically testing the MVC framework.

Upvotes: 1

Related Questions