5

私は RSpec2 v2.13.1 を使用していますが、rspec-mocks ( https://github.com/rspec/rspec-mocks ) を含める必要があるようです。確かに、それは私の Gemfile.lock にリストされています。

ただし、テストを実行すると、

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

実行しようとしているテストは次のとおりです。

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

目標は、通知機能とメッセージのおとりをスタブ化して、CommentEvent クラスが実際に前者を呼び出しているかどうかをテストできるようにすることです。スタブは before(:all) ではサポートされていませんが、before(:each) でも機能しないという rspec-mocks のドキュメントを読みました。ヘルプ!

洞察をありがとう...

4

1 に答える 1

4

Notifier、その名前によると、定数です。

allowまたはで定数を 2 倍にすることはできませんdouble。代わりに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)

編集: stub() 呼び出しの構文エラーを修正

于 2013-06-24T16:33:47.013 に答える