This is bothering me for some time now. I put all my validation in a service layer. However when I unit test I us开发者_如何学Cually 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?
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.
If I were you, I would test the service layer-- which contains the validation--- separately instead of via action method.
The reason for this is that I want to keep my test code "does one testing at a time". I have tests for my controllers that test for the interaction of model and view specifically and not something else; and I would like to have tests specifically for service layer and for the validation.
When you are doing unit testing, mocks are just inevitable.
精彩评论