我有一个Rails 3.2应用程序,我试图测试一个方法是否正确触发电子邮件 . 我知道它可以从控制台测试它 . 但是,我无法通过RSpec测试 .
如果昨天有一篇文章尚未经过审核,应该发送一封电子邮件 .
# article.rb
scope :yesterday, where(:date=>Date.yesterday)
scope :not_reviewed, where(:reviewed=>nil)
def self.articles_not_updated
@articles_not_updated = Article.yesterday.not_reviewed
if @articles_not_updated.present?
Mailer.articles_not_updated(@articles_not_updated)
end
end
# article_spec.rb
context "articles not updated" do
it "should send an email if there are articles not reviewed" do
@article = FactoryGirl.create(:article, date: Date.yesterday, reviewed: nil)
Article.articles_not_updated
ActionMailer::Base.deliveries.empty?.should be_false
end
end
我一直收到它返回false的失败消息 . 我也尝试过使用 Mailer.should_receive(:articles_not_updated)
,但该测试也失败了 . 有人可以帮我弄清楚我做错了什么吗?
**** *** Update - Adding Error Message *****
expected: false value
got: true
1 回答
根据我们的聊天室对话,
should_receive
需要在执行预期发送相关消息的代码之前出现 . 这至少解释了为什么你的备用测试失败了 .