我使用的是rspec-mocks v2.13.1,似乎应该包含RSpec2 -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类是否真的调用了前者。我在rspec-mocks文档中读到,在before(:all)中不支持存根,但在before(:each)中也不支持。帮助!
谢谢你的见解。
发布于 2013-06-25 00:33:47
Notifier,顾名思义,是一个常量。
不能用allow或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()调用中的语法错误
https://stackoverflow.com/questions/17280012
复制相似问题