Grant Birchmeier
Grant Birchmeier

Reputation: 18484

Upgrading to RSpec 3 and following deprecation notice results in "undefined method 'allow'"

Before upgrading RSpec, I had this block in my features/support/hooks.rb file:

After do
  begin
    Challenge.unstub(:current)
  rescue RSpec::Mocks::MockExpectationError
  end
end

After upgrading, I got this notice:

DEPRECATION: Using unstub from rspec-mocks' old :should syntax without explicitly enabling the syntax is deprecated. Use allow(...).to_receive(...).and_call_original or explicitly enable :should instead. Called from /Users/grant/xx/features/support/hooks.rb:37:in block in <top (required)>.

Ok, sounds pretty straightforward. I changed my code to this:

After do
  begin
    allow(Challenge).to receive(:current).and_call_original
  rescue RSpec::Mocks::MockExpectationError
  end
end

But now I'm getting:

undefined method allow for #<Cucumber::Rails::World:0x007facbed9f1d0> (NoMethodError)

Wat? C'mon RSpec, I did exactly what you told me to do!

Based on some Googling, I tried adding require 'rspec/expectations' to the top of this file. It didn't do anything.

Can anyone fill me in on what I'm missing?

Upvotes: 2

Views: 498

Answers (2)

Grant Birchmeier
Grant Birchmeier

Reputation: 18484

It turns out that what I'm doing is completely unnecessary, and that's probably why allow is not defined for the After context.

Rspec-mock stubs are removed after every example (in Rspec) or scenario (in cucumber), so unstubbing them in the After block is redundant and useless.

The whole After block that I'm trying to correct should instead simply be deleted.

(Special thanks to this post by Myron Marston on the Rspec Google Group)

Upvotes: 1

Dave Schweisguth
Dave Schweisguth

Reputation: 37617

Perhaps your RSpec configuration doesn't enable the allow syntax. In your RSpec configuration file, probably spec/spec_helper.rb or spec/rails_helper.rb, do you have something like the following?

RSpec.configure do |config|
  config.mock_with :rspec do |c|
    c.syntax = :should
  end
end

If so, see whether changing it to

RSpec.configure do |config|
  config.mock_with :rspec do |c|
    c.syntax = [:should, :expect]
  end
end

fixes your problem. And then replace all uses of should with uses of allow, and upgrade other uses of the :should syntax, perhaps with transpec, and disable the :should syntax, and you'll be up to the minute.

Source: https://relishapp.com/rspec/rspec-expectations/docs/syntax-configuration

Upvotes: 1

Related Questions