简体   繁体   English

Rails 5-测试应用程序邮件

[英]Rails 5 - testing the application mailer

I want to test the mailer in my application to make sure it is doing what I want it to do. 我想在我的应用程序中测试邮件程序,以确保它正在执行我想要的操作。

class LessonMailer < ApplicationMailer
   def send_mail(lesson)
      @lesson = lesson
      mail(to: lesson.student.email, 
        subject: 'A lesson has been recorded by your tutor')
   end
end 

This is my test in the spec/mailers directory 这是我在spec / mailers目录中的测试

require "rails_helper"

RSpec.describe LessonMailer, :type => :mailer do
   describe "lesson" do

     let( :student ){ FactoryGirl.create :user, role: 'student', givenname: 'name', sn: 'sname', email: 'test@sheffield.ac.uk' }
     let( :lesson  ){ FactoryGirl.create :lesson, student_id: 2 }
     let( :mail    ){ LessonMailer.send_mail( lesson ).deliver_now 

     it "renders the headers" do
      expect(mail.subject).to eq("A lesson has been recorded")
      expect(mail.to).to eq(["to@example.ac.uk"])
      expect(mail.from).to eq(["no-reply@example.ac.uk"])
     end

    it "renders the body" do
     expect(mail.body.encoded).to match("A lesson form has been recorded")
    end
  end
end

I want to test that the 'send_mail' method is working the way I want it to however, I am getting this error. 我想测试'send_mail'方法是否按我想要的方式工作,但是,我收到了此错误。 How do I go about solving this problem ? 我该如何解决这个问题? Thank you. 谢谢。

 NoMethodError:
   undefined method `email' for nil:NilClass
 # ./app/mailers/lesson_mailer.rb:4:in `send_mail'

So, with FactoryGirl, you just need to instantiate the different objects that you need. 因此,使用FactoryGirl,您只需要实例化所需的不同对象。 Reading your code, it seems clear that a lesson has a student and that students have an email . 阅读您的代码,很明显一lesson有一个student ,而学生们有一封email So go ahead and create everything you need and then call your method. 因此,继续创建所需的一切,然后调用您的方法。 You can do something like this: 您可以执行以下操作:

# Here's the student factory (for this use case, you'll probably want to make it more general)
FactoryGirl.define do
  factory :user do
    role 'student'
    givenname 'name'
    sn 'sname'
    email 'test@sheffield.ac.uk'
  end
end

# Here's your test
require "rails_helper"

RSpec.describe LessonMailer, :type => :mailer do
  describe "lesson" do
    let( :student ){ create :student, email: 'test_email@example.com' }
    let( :lesson  ){ create :lesson, student: student }
    let( :mail    ){ LessonMailer.send_mail( lesson ) }

    it ' ... ' do
      ...
    end
  end
end

You'll need to let the test environment know that you want the emails to be delivered to the ActionMailer::Base.deliveries array. 您需要让test环境知道您希望将电子邮件传递到ActionMailer :: Base.deliveries数组。 To do this, make sure that 为此,请确保

config.action_mailer.delivery_method = :test

is set in your config/environments/test.rb 在您的config/environments/test.rb

One last thing, I'm not sure if you'll need it, but you might have to call the mailer with, .deliver_now . 最后一件事,我不确定您是否需要它,但是您可能必须使用.deliver_now来调用邮件程序。

Like this: 像这样:

let( :mail ){ LessonMailer.send_mail( lesson ).deliver_now }

... or it may not send. ...否则可能无法发送。 I can't remember off the top. 我不记得了。

Let me know how it goes. 让我知道事情的后续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM