C# – Where should you unit test your validation

asp.net-mvccnetunit testing

This is bothering me for some time now. I put all my validation in a service layer. However when I unit test I usually pass everything through action method. Then that goes into my service layer what contains the validation.

So I am not sure now if that is the best way to do it. Since usually they say you should just test that method.

So what do you guys think?

Best Answer

If you're passing everything through your action request then it sounds like you are doing integration testing.

If you are doing (unit) testing then you should be testing units. In this case you would pass in all the data required to your service layer to (simulate) the action request.

You should mock up the object that is passed to the service layer, pass it and then Assert the expected results against what you actually got back.

EDIT

Just as an addition, it's great to have the end-to-end, or integration, tests because it proves that the (process) works.

However, you have to have the unit tests because they will test the individual components and will zero you in on defects quicker than end-to-end tests can or will.