【发布时间】:2015-04-12 16:46:07
【问题描述】:
我正在尝试测试 devise 是否正在发送确认电子邮件。这对我来说变得有点挑战,因为测试在 Rails 中使用了不同的环境,我不太确定我是否走对了路。 这是我的 rspec 测试。
describe "Sign up:", :type => :feature do
before(:each) do
@user = FactoryGirl.build(:user)
visit root_path
click_link "Sign Up"
fill_in "Name", :with => @user.name
fill_in "Email", :with => @user.email
fill_in "Password", :with => @user.password
fill_in "Password confirmation", :with => @user.password
click_button "Sign up"
end
describe "user gets a confirmation email" do
subject { ActionMailer::Base.deliveries.last }
it { is_expected.to deliver_to(@user.email) }
end
这是我运行规范时的消息。
Failure/Error: it { is_expected.to deliver_to(@user.email) }
NoMethodError:
undefined method `perform_deliveries' for nil:NilClass
我想测试这个功能,但是 Rails 可以在测试环境中发送电子邮件吗?如果是这样,什么样的代码使这种类型的代码通过?我将邮件程序设置为 sendgrid,所以我也可以显示它。 Devise 的用户设置为可确认,因此应该会发送一封确认电子邮件。
【问题讨论】:
-
问题可能是您的邮件程序正在异步交付,并且交付实际上是在您的预期之后执行的。 IE。
sleep 1在您的期望可能会解决您的问题之前(尽管这被广泛认为是一种反模式)
标签: ruby-on-rails devise capybara mailer