views:

308

answers:

2

So I have decided to investigate the state of Rails' testing. After reading a few accepted answers here on SO talking about how Rails' testing documentation is atrocious I am starting to agree. Thus far, I am apparently not in the club of people that understand Rails testing- even though I understand the framework quite well, or so I thought.

I've read that Shoulda is a good plugin to use so I'm using it. My first test: "ensure method requires an ID parameter"... um sure this makes sense; I want to make sure that my before_filter :validate_id on my controller actually works. This is step 1 of the 3 required to test that my index's GET works the way it is supposed to. FAIL! Clearly the test environment runs differently than the normal Rails stack:

...
lib/action_controller/base.rb:524:in `process_without_filters'
...

checking everywhere I can results in practically no information on including filters when ActionController::TestCase uses get {}. Where do I join "the club of Rails testers" so I can figure this out?

  context "GET :index requires id" do

    setup {      
      get :index
    }

    should_redirect_to('/') { }

  end
A: 

Are you sure that failure has anything to do with before_filters? I don't think it does. Look further up the stack for other errors... I'm able to test my app with login requirements no problem.

You're asserting that index requires an id...but requesting it without one. Is it throwing an error because there is no id? Maybe you need to catch that, or test the failure mode with assert_raises.

Andrew Vit
My filter detects whether an id is present and valid. If not it redirects to "/". I'm sure that this is the problem, as the error I get when I run the test is a result of the object generated by the id/filter being nil.
Bill
If you're getting an error when you run the test, your test isn't necessarily broken. Tests fail due to both code errors and logic, if you write tests you're going to see a lot of both. Look closely at the error, it should tell you (though sometimes indirectly) what's wrong and what to to do to fix it. This seems like a straightforward situation; if you post the error, it shouldn't be too hard to figure out what's gone wrong.
chrisdinn
A: 

I agree with Andrew that the problem probably lies elsewhere. I have plenty of functional tests that work with controllers with before_filters, and have never run into the problem you specify.

insane.dreamer