gpt4 book ai didi

ruby-on-rails - 为邮件 rails 编写测试

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:42 25 4
gpt4 key购买 nike

我正在尝试测试一个邮件发送方法,它应该只向经过验证的用户发送邮件。

所以我的邮件程序中有这样一个方法:

def send_hints(user)
@user = user
mail :to => user.email, :subject => "Your hints for the day"
end

我正在努力确保只有经过验证的用户才能收到此信息。所以 user.verified_at 不是 null/nil。

现在开始写测试:

describe "Emails should be sent only to verified user"
let(:user) { FactoryGirl.create(:user, :verified_at => DateTime.now) }
let(:mail) { UserMailer.send_hints(user) }
...

我不确定在这里断言什么是明智的?如果我以相反的方式发送邮件,则应该发送或不应该发送邮件。

最佳答案

这是一种测试方法:

# config/environments/test.rb
YourApplication::Application.configure do
config.action_mailer.delivery_method = :test
end


# spec/your_spec.rb
describe 'Emails should be sent only to verified user' do
let(:user) { FactoryGirl.create(:user, verified_at: verified_at) }
before { UserMailer.send_hints(user) }
subject { ActionMailer::Base.deliveries.last.to }
context 'When the user is verified' do
let(:verified_at) { DateTime.now }
it { should include(user.email) }
end
end

context 'When the user is not verified' do
let(:verified_at) { nil }
it { should_not include(user.email) }
end
end

关于ruby-on-rails - 为邮件 rails 编写测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846791/

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