gpt4 book ai didi

ruby-on-rails - rspec email_spec 和 jobs.. 它是如何工作的?我想检查这封信是否已发送..可能还有电子邮件正文

转载 作者:行者123 更新时间:2023-11-28 20:51:59 26 4
gpt4 key购买 nike

我有'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

我如何进行测试?谢谢

我看到了“email-spec”,但我的代码无法理解它

我有'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@222@mail.ru"

@user = User.new
@user.email = "2@2.ru"
@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 和 jobs.. 它是如何工作的?我想检查这封信是否已发送..可能还有电子邮件正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42815978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com