Shoulda-matchers: undefined local variable or method `set_flash'

Created on 16 Feb 2016  路  2Comments  路  Source: thoughtbot/shoulda-matchers

Here are some simplified snippets of my test environment:

group :development, :test do
  gem 'rspec-rails' # (3.4.2)
end

group :test do
  gem 'shoulda-matchers' # (3.1.1)
end
RSpec.describe GitRepositoryLocationsController do
  describe 'POST #create', :logged_in do
    before { post :create, params }
    context 'when the GitRepositoryLocation is invalid' do
      let(:params) { ... }
      it { is_expected.to set_flash }
    end
  end
end

But I get the error

undefined local variable or method `set_flash' for #<RSpec::ExampleGroups::GitRepositoryLocationsController::POSTCreate::WhenTheGitRepositoryLocationIsInvalid:0x007fc371af87a0>

It works when I include the module in the spec file

include Shoulda::Matchers::ActionController

Most helpful comment

Hi @dideler,

Perhaps you've not configured Shoulda correctly? Have you added the following in your spec/rails_helper.rb, for example?

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

The Getting Started Guide has more info.

Do other shoulda-matchers fail to work too, or is it just set_flash?

All 2 comments

Hi @dideler,

Perhaps you've not configured Shoulda correctly? Have you added the following in your spec/rails_helper.rb, for example?

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec
    with.library :rails
  end
end

The Getting Started Guide has more info.

Do other shoulda-matchers fail to work too, or is it just set_flash?

That solves it, thanks. Missed that step somehow.

Was this page helpful?
0 / 5 - 0 ratings