【发布时间】:2011-09-02 14:20:57
【问题描述】:
我的模型(RoR 3.0.x)中的代码或多或少是这样的:
class Message
after_create :notify
protected
def notify
if visible?
Notifier.message_from_portfolio( user, self ).deliver
else
Notifier.invisible_message_from_portfolio( user, self ).deliver
end
end
end
我正在使用最新的 rspec gem 来测试它。 问题是我无法测试 notify 方法:如果我直接测试它,我不能因为它受到保护,如果我创建一条消息并设置期望它不起作用,因为显然即使 rspec 运行 notify 方法我无法及时接听电话。
我的规格是:
describe :notification do
it "should send the whole message by email when visible" do
u = Factory.create( :user, :account_type => 1 )
message = u.messages.build( :body => "Whatever", :author => "Nobody", :email => "test@example.com" )
Notifier.should_receive( :message_from_portfolio )
message.save
end
end
对象通知程序永远不会收到 message_from_portfolio。我究竟做错了什么?有什么建议吗?
【问题讨论】:
标签: ruby-on-rails-3 rspec rspec2 rspec-rails