【发布时间】:2011-02-16 19:05:57
【问题描述】:
我正在编写一个简单的评论观察器,它会在创建新评论时触发邮件程序。所有相关代码都在这个要点中:https://gist.github.com/c3234352b3c4776ce132
请注意,Notification 的规范通过,但 CommentObserver 的规范失败,因为 Notification.new_comment 正在返回 nil。我发现我可以通过使用它来获得通过规范:
describe CommentObserver do
it "sends a notification mail after a new comment is created" do
Factory(:comment)
ActionMailer::Base.deliveries.should_not be_empty
end
end
然而,这并不理想,因为它在观察者的规范中测试邮件程序的行为,而我真正想知道的是它正确地触发了邮件程序。为什么邮件程序在原始版本的规范中返回nil?指定此类功能的最佳方法是什么?我正在使用 Rails 3 和 RSpec 2(还有 Factory Girl,如果这很重要的话)。
【问题讨论】:
标签: ruby-on-rails rspec observer-pattern actionmailer