我有'controllers / users_controller.rb'

def create
   user = User.new(person_params)
   if user.save
     EmailSendJob.perform_later(user.email, 'random_password')
   end
 end


我有'mailers / user_mailer.rb'

def user_new(user_to, password)
  @password = password
  mail to: user_to, subject: "Password for you"
end


我有'jobs / email_send_job.rb'

def perform(email, password)
  UserMailer.user_new(email, password).deliver_now
end


我该如何测试?谢谢

我看到了“电子邮件规范”,但我的代码无法理解

我有'spec / controllers / users_controller_spec.rb'

require "spec_helper"
require "rails_helper"
require "email_spec"

describe UsersController do
  User.delete_all

  before(:all) do
    @email_for_test = "222@[email protected]"

    @user = User.new
    @user.email = "[email protected]"
    @user.name = "test"
    @user.save
  end


  it "create" do
    post :create, params: {user: {email: @email_for_test,
                                  name:"test",
                                  description: "test"}}
    user = User.last
    expect(user.email).to eq(@email_for_test)

    last_delivery = ActionMailer::Base.deliveries.last
    last_delivery.body.raw_source.should include "This is the text of the email"

  end

end


ActionMailer :: Base.deliveries.last不起作用
也许你有例子。我会很感激

最佳答案

尝试使用ActionMailer::Base.deliveries.count

因此,例如:

  it "sends an email" do
    expect { UserMailer.user_new(email, password).deliver }.to change { ActionMailer::Base.deliveries.count }.by(1)
  end

关于ruby-on-rails - rspec email_spec和作业..它是如何工作的?我想检查这封信是否已发送..也许是电子邮件正文,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42815978/

10-14 01:34