我正在尝试使用 RSpec 测试方法是否会在 Rails 3.2 中触发电子邮件。它适用于生产和开发,但我无法让它通过我的测试。
我收到错误undefined method 'deliver' for nil:NilClass
# user_spec.rb
it "should send an email if user has weekly email set to true" do
@user = FactoryGirl.create(:user, user_name: "Jane Doe", email: "jane@gmail.com", weekly_email: true, weekly_article_count: 10)
Mailer.should_receive(:weekly_email)
User.send_weekly_email
end
# user.rb
scope :weekly_email, where(:weekly_email => true)
scope :weekly_article_count, :conditions => ["weekly_article_count IS NOT NULL"]
def self.send_weekly_email
@users = User.weekly_email.weekly_article_count
@users.each do |user|
Mailer.weekly_email(user).deliver
end
end
我也尝试过使用Mailer.should_receive(:weekly_email).with(user)
,但后来我收到错误undefined local variable or method 'user'