shoulda-matchers icon indicating copy to clipboard operation
shoulda-matchers copied to clipboard

NoMethodError: undefined method `response_code' for nil:NilClass

Open abhinavramesh8 opened this issue 7 years ago • 9 comments

I am using shoulda-matchers 3.1.2 and shoulda-context 1.2.2 along with Rails 5.2 and Minitest 5.11.3 . Ruby version is 2.5.0 . The following is the code corresponding to the controller test for the posts controller:

class PostsControllerTest < ActionDispatch::IntegrationTest

context "GET #index" do
  setup do
    get posts_url
  end

  # this works
  should "respond with :success" do
    assert_response :success
  end

  # this results in a NoMethodError
  should respond_with(:success)
end

end

The test without the shoulda matcher works fine while the one that uses the matcher results in the following error:

PostsControllerTest#test_: GET #index should respond with 200. : NoMethodError: undefined method `response_code' for nil:NilClass /usr/local/lib/ruby/gems/2.5.0/gems/shoulda-context-1.2.2/lib/shoulda/context/context.rb:346

Why is this happening ? Does shoulda-matchers currently only work with controller tests that inherit from ActionController::TestCase instead of ActionDispatch::IntegrationTest ?

abhinavramesh8 avatar Jun 25 '18 18:06 abhinavramesh8

Hmm... my gut response is no, because that's not what it was designed to work with, but I'm not immediately sure how these controller matchers work anyway with shoulda-context, so I'll look into that for you when I get a chance.

mcmire avatar Jun 25 '18 20:06 mcmire

reproduced with shoulda (3.6.0), shoulda-context (1.2.2), shoulda-matchers (3.1.2), minitest (5.11.3), rails (5.1.6), ruby 2.3.8

UniIsland avatar Nov 18 '18 14:11 UniIsland

i also have this error using shoulda-matchers 3.1.3, rspec-rails 3.8.2, rails 5.2.2, ruby 2.5.3

units_controller_spec.rb

RSpec.describe UnitsController, type: :controller do
  describe "GET #index" do
    context "without user token" do
      it { should respond_with(403) }
    end
    context "with user token" do
      it { should respond_with(200) }
    end
  end
end

output:

  1. UnitsController GET #index without user token should respond with 401 Failure/Error: it { should respond_with(403) }

    NoMethodError: undefined method 'response_code' for nil:NilClass # ./spec/controllers/units_controller_spec.rb:6:in block (4 levels) in <top (required)>'

  2. UnitsController GET #index with user token should respond with 200 Failure/Error: it { should respond_with(200) }

    NoMethodError: undefined method 'response_code' for nil:NilClass # ./spec/controllers/units_controller_spec.rb:9:in block (4 levels) in <top (required)>'

askareija avatar Feb 02 '19 13:02 askareija

@askareija It doesn't look like you're making a request, so there's no response object. Try adding this at the top of your example group:

before { get :index }

mcmire avatar Feb 04 '19 16:02 mcmire

@mcmire oh yea how can i forgot that, thank you it's working now

askareija avatar Feb 07 '19 07:02 askareija

Hey folks. In an effort to lighten our load as maintainers and be able to serve you better in the future, the shoulda-matchers team is working on cleaning out the cobwebs in this repo by pruning the backlog. As there are few of us, there are a lot of items that will simply never earn our attention in a reasonable time frame, and rather than giving you an empty promise, we think it makes more sense to focus on more recent issues. That means, unfortunately, that we must close this issue.

Don't take this the wrong way: our aim is not to diminish the effort people have made or dismiss problems that have been raised. If you feel that we should reopen this issue, then please let us know so that we can reprioritize it. Thanks!

mcmire avatar May 06 '20 07:05 mcmire

I'm having the same problem with shoulda 5.0.0.rc1, shoulda-context 2.0.0, shoulda-matchers 5.3.0, minitest 5.18.0, rails 7.0.4, ruby 3.1.0

dfl avatar May 28 '23 22:05 dfl

I'm having the same problem with shoulda 5.0.0.rc1, shoulda-context 2.0.0, shoulda-matchers 5.3.0, minitest 5.18.0, rails 7.0.4, ruby 3.1.0

Mee too!

shayani avatar Aug 27 '23 17:08 shayani

@shayani FYI I rolled my own, also with some CSS matchers https://gist.github.com/dfl/58c00d8732b633b869b8686c8db1696e for example: should_respond_with :success, matching: { "p#main.foo": ->{ /some {@thing}/ }, /unwanted/ => false }

dfl avatar Aug 27 '23 19:08 dfl