Shoulda Test in Rails 3: should respond_with :success

1.2k views Asked by At

I have some troubles with testing in Rails 3. I'm currently upgrading a Rails2 app to Rails3. I'm using shoulda for testing. In my functional tests, I'm testing with shoulda, that a GET should respond with success

context "GET to :blame" do
  should "mark a song as blamed" do
    get :blame, :id => @song.id
    assert_equal Blame.count, 1
    get :blame, :id => @song.id
    assert_equal Blame.count, 2
  end
  should respond_with :success
end

In the next to last line I'm getting the following error while executing the functional tests with rake test:functionals:

  1) Error:
test: a visitor GET to :blame should respond with 200. (SongsControllerTest):
NoMethodError: undefined method `response_code' for nil:NilClass
    /Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
    /Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:57:in `response_code'
    /Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:48:in `correct_status_code?'
    /Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/action_controller/matchers/respond_with_matcher.rb:30:in `matches?'
    /Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/assertions.rb:53:in `assert_accepts'
    /Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:324:in `block in should'
    /Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `call'
    /Users/23tux/.rvm/gems/ruby-1.9.2-p136@rails3/gems/shoulda-2.11.3/lib/shoulda/context.rb:382:in `block in create_test_from_should_hash'

I'm using Ruby 1.9.2, Rails 3.0.3 and Shoulda 2.11.3. Hope, someone can help me.

thx, tux

1

There are 1 answers

0
Mihai A On BEST ANSWER

The two should blocks are run separately and no request would be made for respond_with hence the error. You would need to make the request in a setup block such as:

context "GET to :blame" do
  setup do
    get :blame, :id => @song.id
  end
  should "mark a song as blamed" do
    assert_equal Blame.count, 1
  end
  should respond_with :success
end