私は 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 のドキュメントを読みました。ヘルプ!
洞察をありがとう...