Let's say I want to test a controller that gets some value from a service and then pass some of all of those values to the view.

Do you test that the view gets the result that the controller took from the service, without taking care of seeing the exact variables being passed, or do you test specifically for the variables that you need on the view?

So if in the view you have var 'foo' and 'foo2', you can test that the controller is simply passing the info it gets from the service, or you can test that the variables being passed are effectively called 'foo' and 'foo2'.

What approach do you use?

2 Answers 2


Whenever you test a class or a method, you provide the inputs and then verify that it produces the outputs that it is responsible for.

If the documented behaviour of your class is to pass on whatever it gets, then you would write a test passing in "foo" and "bar", or "grumpy" and "slouchy", or any other set of inputs, and verify that they are passed on. The check that "foo" is produced belongs into another test (the test for the service).

But if the behaviour requires that the controller sends values for particular set of attributes, then your test should verify that it produces exactly this output, and check both the attributes and their values.


This is where you have to apply a combination of dependency injection and mock tests. You would test the service and controller & view separately. Do this by mocking the service to return a recorded value and then test if you controller end up with this recorded value.

TDD is unit testing. You shouldn't test the coupled configuration (which is integration testing). Test the 2 modules separately as explained above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.