Rspec-mocks 'allow' returns an undefined method

I am using RSpec2 v2.13.1 and it seems that rspec-mocks ( https://github.com/rspec/rspec-mocks ) should be included in it. Of course, this is stated in my Gemfile.lock.

However, when I run my tests, I get

Failure/Error: allow(Notifier).to receive(:new_comment) { @decoy } NoMethodError: undefined method `allow' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fc302aeca78> 

Here is the test I'm trying to run:

 require 'spec_helper' describe CommentEvent do before(:each) do @event = FactoryGirl.build(:comment_event) @decoy = double('Resque::Mailer::MessageDecoy', :deliver => true) # allow(Notifier).to receive(:new_comment) { @decoy } # allow(Notifier).to receive(:welcome_email) { @decoy } end it "should have a comment for its object" do @event.object.should be_a(Comment) end describe "email notifications" do it "should be sent for a user who chooses to be notified" do allow(Notifier).to receive(:new_comment) { @decoy } allow(Notifier).to receive(:welcome_email) { @decoy } [...] end 

The goal is to drown out the notifier and messages so that I can check if my CommentEvent class actually calls the first. I read in the rspec-mocks documentation that stubbing is not supported in before (: all), but it does not work in before (: each). Help!

Thanks for any info ...

+4
source share
1 answer

Notifier , according to its name, is a constant.

You cannot double a constant with allow or double . Instead, you need to use stub_const

 # Make a mock of Notifier at first stub_const Notifier, Class.new # Then stub the methods of Notifier stub(:Notifier, :new_comment => @decoy) 

Edit: Fixed syntax error when calling stub ()

+3
source

Source: https://habr.com/ru/post/1487920/


All Articles