Search code examples
ruby-on-railsrspecactionmailerrspec-rails

Rails: Testing that an Email Sends with RSpec


I have a Rails 3.2 app where I'm trying to test to see if a method properly triggers an email. I know that it works from testing it from the console. However, I can't get my test to pass the RSpec test.

What should happen is if there is an Article from yesterday that has not been reviewed yet, it should send an email.

# 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

I keep getting the failure message that it returns false. I have also tried using Mailer.should_receive(:articles_not_updated), but that test also fails. Can someone please help me figure out what I'm doing wrong?

* Update - Adding Error Message ***

expected: false value
got: true

Solution

  • Per our chatroom conversation, should_receive needs to appear prior to executing the code that is expected to send the message in question. This at least explains why your alternate test was failing.