gpt4 book ai didi

ruby-on-rails - 使用参数测试邮件程序的最佳方法

转载 作者:行者123 更新时间:2023-12-02 19:20:21 24 4
gpt4 key购买 nike

我有一个邮件程序传递这样的参数:

AnimalMailer.daily_message(owner).deliver_later

方法如下所示:

动物邮件

class AnimalMailer < ApplicationMailer

def daily_message(owner)
mail(
to: "#{user.name}",
subject: "test",
content_type: "text/html",
date: Time.now.in_time_zone("Mountain Time (US & Canada)")
)
end
end

我是编写规范的新手,想知道如何将所有者传递给方法并对其进行测试。我目前有这个设置:

require "rails_helper"

RSpec.describe AnimalMailer, type: :mailer do

describe "monthly_animal_message" do
let(:user) { create(:user, :admin) }

it "renders the headers" do
expect(mail.subject).to eq("test")
expect(mail.to).to eq(user.name)
end
end
end

最佳答案

规范通常遵循三步流程:1) 设置,2) 调用,3) 期望。这适用于像其他任何东西一样的单元测试邮件程序。测试中的调用和参数与一般用途相同,因此在您的情况下:

RSpec.describe AnimalMailer, type: :mailer do  
describe "monthly_campaign_report" do
let(:user) { create(:user, :admin) }
let(:mail) { described_class.daily_message(user) } # invocation

it 'renders the headers' do
expect(mail.subject).to eq('test')
expect(mail.to).to eq(user.name)
end

it 'renders the body' do
# whatever
end
end
end

请注意,由于 describe 是被测试的类名,您可以从那里使用 described_class 来引用所描述的类。您也可以随时使用 AnimalMailer.daily_message,但除其他事项外,described_class 确保如果您随机播放或分享示例,您始终在测试您认为的自己。

另请注意,在对邮件程序进行单元测试时,您主要关注内容的正确生成。在作业、 Controller 等中成功交付或使用的测试将作为请求或功能测试的一部分完成。

关于ruby-on-rails - 使用参数测试邮件程序的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63143981/

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