shoulda-matchers: Undefined method respond_with for namespaced controller spec

I have a namespaced controller for an Api, and when i use the should respond_with matcher in the spec for the same , i get the message undefined method respond_with’ for #RSpec::ExampleGroups::ApiV1UsersController::GETShow:0x007fd30e4a5c20`

The spec i have is as follows :

describe Api::V1::UsersController, type: :controller do
  describe "GET #show" do
    before(:each) do
      @user = create(:user)
      get :show, id: @user.id, format: :json
    end

   it {  should respond_with 200 }
  end
end

I have added the rspec-rails gem before the shoulda-matchers gem in my Gemfile. Also, my users controller is located in app/controllers/api/v1/users_controller.rb and the spec in spec/controllers/api/v1/users_controller_spec.rb .

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20

Most upvoted comments

I have found this error before

and found this one about configure (that I thinks newly add in new versions) https://github.com/thoughtbot/shoulda-matchers#configuration

so I add this to spec_helper.rb

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    with.test_framework :rspec
    with.library :rails
  end
end

hope this help

Had the same problem, but following the tip from @fake-or-dead it was solved.

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    # Choose a test framework:
    with.test_framework :rspec
    with.library :rails
  end
end

But according to the link https://github.com/thoughtbot/shoulda-matchers#rspec it must go in rails_helper.rb