views:

130

answers:

3

I am currently beginning with BDD - I have not written any tests before. I always try to keep my models fat and my controllers skinny.

What do you think - are controller specs necessary?

Best regards

+2  A: 

Yes. Test the correct calls are being made, and that the correct redirects and being made when necessary, and test that the correct pages are being rendered. So test that your application behaves as expected.

railsninja
Many parts of visual behaviour I am already testing via Cucumber.
A: 

For myself I'm trying to understand the balance between seeking high levels of code coverage in testing and the long term cost of maintaing a set of tests that may be quite brittle.

Suppose we have a Controller that goes

    try {
        result = mode.doSomething();

        if  (result.count == 0 )
             message = "none found"
             redisplay criterion page
        else if (result.count == 1 ) 
             display detail page
        else 
             display list page
    } catch exception {
        message = "bad things happened, please try again"
        redisplay criterion page
    }

A tentative thought is is that tests of the three count cases (0, 1, many) may be less valuable, and more prone to change than the test of the exeption case. Less valuable because a). other testing will catch problems in page display b). the test gets very close to simply reproducing the code.

    code: go to page X
    test: did you go to page X?

If the developer makes the wrong choice of X he gets the test wrong too! If usability testing of the UI reveals that Y is better page to display then we update the code and the test in tandem. Did the test really achieve anything?

Whereas the exception case may be very hard to exercise in normal UI testing, and really easy to test with mocks. And, behaviour following exceptions is something that we really do need to get right.

djna
I assume you wrote the test for redirecting to X first. When you change where the action is redirected, you change the test first, and then you make it pass. It's much faster to verify this than it is to log into the app and see if it worked. Even with cucumber, a spec / test still runs faster than the setup and crawl of your features.I am a firm believer in "if you write a line of code, first write a failing unit test". HOWEVER, if you want to avoid those, use an abstraction like inherited_resources and stop writing both that code AND tests.
Brian Hogan
I do pretty much agree with you, but to play devil's advocate for a moment. At some time we were going to need to run that app to test the aesthetics, usuability etc. Defects in redirection would have been picked up then. Are we duplicating effort? Are we finding enough defects to warrant the effort? Suppose we transgressed (or even by formally documented policy) didn't write tests for three lines of code before we wrote them how great would the impact be?
djna
Manual testing may uncover defects, but why let the testers see stuff you can easily catch with rspec + cucumber? You can easily automate many different test cases, and you can add new ones when an end user does find a flaw. Remember these tests are not just for when you're building an app, they're also to help you maintain it later, especially when you want to do a framework update.
Brian Hogan
The devil's advocate says: because the testers will anyway, "easily" versus do nothing. The tests are of course as much for maintenance as initial development, and therefore have a maintainance cost, the cases churn as the UI changes, many (every?) "failure" is fixed by mods to the test case, we never actually catch any real bugs. A new version of Struts or whatever is not going to break the code that goes "if result == 2 go to page a", and we **are** going to unit test that result **is** 2.
djna
A: 

What do you think - are controller specs necessary?

I don't think they are necessary if you combine them with a good integration test. I find the usability of the application very important, so for every change in controller / views, I click through the application and find out how it feels. Creating 'dumb' tests for controllers and views doesn't have much extra value in my opinion.

For integration testing I'm using Cucumber. This makes it possible to test the full stack and make sure your application performs in the way you want. For the business logic (the fat models) I still use RSpec.

Edwin V.