5

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...

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121

1 Answers1

4

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

dyslesia
  • 28
  • 1
  • 4
Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • On what basis are you saying that `allow` and `double` are the same? They don't appear to be the same based on either the documentation or the source. – Peter Alfvin Jun 24 '13 at 16:59
  • @PeterAlfvin,check this: https://github.com/rspec/rspec-mocks#method-stubs " rspec-mocks supports 3 forms for declaring method stubs" – Billy Chan Jun 24 '13 at 17:04
  • @PeterAlfvin, the effects are the same according to the example code. Anyway I deleted my last line which doesn't matter to the answer. I rarely use `allow` which seems unnecessary when `double` and `stub` are good enough. – Billy Chan Jun 24 '13 at 17:10
  • I'd actually just read that before asking my question. I guess you're using "the same" in a way that I'm not used to. As I understand it, `allow` specifies behavior for an existing object, while `double` creates a new object and then specifies behavior for that object. I tend to use "the same" when one is an alias for the other or at least when one can be expressed in terms of the other. – Peter Alfvin Jun 24 '13 at 17:23
  • Yes, I noticed the difference, it's not correct to say "same". It's more like `stub`. I don't know what it is different from `stub` as I have not used `allow` before. – Billy Chan Jun 24 '13 at 17:30
  • 3
    this did not solve my problem... and you have a curly brace ending instead of a parathesis. my error is NoMethodError: undefined method `allow' for #,.. sounds like an rspec module needs to be include in that class at runtime... – Mathieu J. Jan 09 '14 at 23:29
  • @shigazaru, would you write a new question with the code details? – Billy Chan Jan 10 '14 at 01:38