Shoulda-matchers: NameError: undefined local variable or method `response' (using the respond_with matcher)

Created on 21 Feb 2014  路  3Comments  路  Source: thoughtbot/shoulda-matchers

I have the following test in spec/routing/reviews_routing_spec.rb:

require "spec_helper"

describe "Routing for", ReviewsController do
  it "responds with 404 on reviews#show with an incorrect url" do
    get("/reviews/unexisting")
    expect(response).to respond_with(404)
  end
end

When running it, I get the following failure:

$ rspec spec/routing/reviews_routing_spec.rb 
F

Failures:

  1) Routing for ReviewsController responds with 404 on reviews#show with an incorrect url
     Failure/Error: expect(response).to respond_with(404)
     NameError:
       undefined local variable or method `response' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000103dfd820>
     # ./spec/routing/reviews_routing_spec.rb:7:in `block (2 levels) in <top (required)>'

Finished in 3.17 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/routing/reviews_routing_spec.rb:4 # Routing for ReviewsController responds with 404 on reviews#show with an incorrect url

This seems to be the correct use case for this method, and it seems similar to what was reported in #150, but there hasn't been any progress on that front.

I even tried pasting the code found in the README verbatim, but I would still get the same error.

When inspecting self.methods inside the it block, I get this list of methods.. response聽is not present in the list.

My spec_helper.rb should also be pretty straightforward. It's here if you'd like to have a look.

Is this a bug, or am I doing something wrong? Are these matchers not supposed to be used outside controller specs? Is there a way to get them working, or should I just move everything to a controller spec?

Most helpful comment

The matcher here is not your problem. response comes from the test itself, shoulda-matchers doesn't provide that.

Your spec is not set up as controller spec but rather routing spec which doesn't provide the response variable. You can either mark the spec as a controller spec or move it to 'spec/controllers' to get that benefit.

https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs

All 3 comments

I just tried it in a controller spec and I get the same issues.

Gem versions from my Gemfile.lock:

shoulda-matchers (2.5.0)
rspec (2.14.1)
  rspec-core (~> 2.14.0)
  rspec-expectations (~> 2.14.0)
  rspec-mocks (~> 2.14.0)
rspec-rails (2.14.1)
  actionpack (>= 3.0)
  activemodel (>= 3.0)
  activesupport (>= 3.0)
  railties (>= 3.0)
  rspec-core (~> 2.14.0)
  rspec-expectations (~> 2.14.0)
  rspec-mocks (~> 2.14.0)
capybara (2.2.1)
  mime-types (>= 1.16)
  nokogiri (>= 1.3.3)
  rack (>= 1.0.0)
  rack-test (>= 0.5.4)
  xpath (~> 2.0)

The matcher here is not your problem. response comes from the test itself, shoulda-matchers doesn't provide that.

Your spec is not set up as controller spec but rather routing spec which doesn't provide the response variable. You can either mark the spec as a controller spec or move it to 'spec/controllers' to get that benefit.

https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs

Was this page helpful?
0 / 5 - 0 ratings