views:

45

answers:

4

I have heard that at the very least, if you write unit tests for only models and try to keep the bulk of your logic in the model, you are pretty well off. Is there any merit to testing controllers and views or any other part of the framework?

A: 

Why shouldn't you test controllers? Ora views? Controllers are classes, actions are methods thus you should test them as any other method.

The entire request dispatching and action performing is in charge of the controller. There can be actions which doesn't involve any model at all so... how do you know if these actions works?

Simone Carletti
views would be constantly changing and would be hard to test usually. controllers shouldn't have logic in them and only should control which pages the user would go to in my opinion.
jimiyash
It's probably better if you start looking at some real world example to better understand how controllers and tests work. See http://goo.gl/qGza and http://goo.gl/FTCn
Simone Carletti
A: 

In both Rails and other similar frameworks (Play etc.) I mostly test models. That is because I find myself mostly putting logic in the model. I also find that the model testing in Rails tends to actually work, whereas when I've tried to write controller tests (functionals) they often are much more fragile and getting them to work is significantly more work than just manually testing the site.

Ideally you should be testing all the code you write, but that isn't always as easy as it needs to be.

Tom Morris
+2  A: 

Use cucumber and test it all at once, dropping to unit tests for specific model/module testing.

David Lyod
+1  A: 

I think that the proper answer is - it depends :-)

If your controller is really dumb and consisted of "one liners" there is no need to test it as there is nothing (or almost nothing) to be broken. From what you've said it's your case. And remember that you should test controllers in isolation so your models should be mocked and it takes some time/code.

Testing views are much trickier because you are suppose to test the layer alone - so you have to mockup all the models / controllers - it's the principle of unit testing.

So I would vote for unit testing models only. You have almost all logic there so your test code coverage is high. And writing RSpec's for model is dead simple.

BUT - apart from unit testing you really should go for integration testing. Cucumber is great for that. In the integration tests you tests all layers at once and not in isolation. And Cucumber tests can be discussed with your customer because they are human readable. It is really great source of clarification - when you write integration test you will often find edge cases that are not specified (aka "what the app should do if the user clicks there?").

So my humble recommendation is simple:

RSpec for unit testing on models, Cucumber for integration testing

pawien