我試圖測試一個方法是否觸發我的Rails 3.2使用RSpec的電子郵件。它在生產和開發工作,我只是無法得到它經過我的測試。Rails的RSpec測試:未定義的方法'交付'零:NilClass
,我發現了錯誤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: "[email protected]", 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'
這應該是正確答案 – MarkJL