I'm using RSpec2 v2.13.1 and it seems that rspec-mocks (https://github.com/rspec/rspec-mocks) should be included in it. Certainly it's listed 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's 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 stub out the notifier and message decoys such that I can test whether my CommentEvent class is indeed invoking the former. I read on rspec-mocks documentation that stubbing is not supported in before(:all), but it doesn't work in before(:each) either. Help!
Thanks for any insights...
Notifier
, according to its name, is a Constant.
You can't double a constant with either 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 on stub() call