views:

96

answers:

2

Hi

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?

+1  A: 

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.

Ngu Soon Hui
+1. Yeah there is a case for integration testing but well before you get to that I think you need to do unit testing. that way if you make a change anywhere it's picked up quicker than from integration testing.
griegs
+2  A: 

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.

griegs
So then should I not test the view? Like what I have is a have basically a method that would be called like Create() in a if statement this would do the validation and create something then return true. Then I just return something. So would there be anything to test in the view then? Like I am assuming I would mockup that Create Method then since I am not testing it. so really it is just one if statement that returns somethingon true.
chobo2
In this case I probably wouldn't bother with testing the view. TDD is something that you need to design yourself so that it best fits your application. There's no 1 formula for all applications. Decide what you need to write tests for so that at any time you should be able to run them and identify any issues arisen from code changes elsewhere in your application. www.dnrtv.com has a great show by Venkat on TDD. I highly recommend viewing it.
griegs