在功能规范中,如何测试成功调用Devise邮件程序?

时间:2016-03-29 12:44:19

标签: ruby-on-rails rspec devise

我有一个用户注册功能测试。如何测试Devise确认说明是否正确发送?我不需要测试电子邮件的内容,只需要调用邮件程序。

我正在后台发邮件。

#user.rb    
def send_devise_notification(notification, *args)
  devise_mailer.send(notification, self, *args).deliver_later
end

我尝试了一些适用于其他邮件程序的方法,包括

it "sends the confirmation email" do
  expect(Devise.mailer.deliveries.count).to eq 1
end

it "sends the confirmation email" do
  message_delivery = instance_double(ActionMailer::MessageDelivery)
  expect(Devise::Mailer).to receive(:confirmation_instructions).and_return(message_delivery)
  expect(message_delivery).to receive(:deliver_later)
end

没有一个像Devise消息那样正常工作。

我做错了什么?

修改

功能规格如下所示:

feature "User signs up"  do
  before :each do
    visit '/'
    click_link 'Sign up'
    fill_in 'user_email', with: valid_attributes[:email]
    fill_in 'user_password', with: valid_attributes[:password]
    fill_in 'user_password_confirmation', with: valid_attributes[:password]
    click_button 'Sign up'
  end
  it "sends the confirmation email" ...

end

1 个答案:

答案 0 :(得分:0)

由于您正在执行高级功能规范,因此我要下注,因为单击'Sign up'按钮,您要确认的是已将一个电子邮件作业添加到队列中。

为此,您可能需要稍微更改您的规格设置:

feature "User signs up" do
  before :each do
    visit '/'
    click_link 'Sign up'
    fill_in 'user_email', with: valid_attributes[:email]
    fill_in 'user_password', with: valid_attributes[:password]
    fill_in 'user_password_confirmation', with: valid_attributes[:password]
  end

  it "queues up a confirmation email job" do
    expect { click_button 'Sign up' }.to \
      have_enqueued_job(ActionMailer::DeliveryJob)
  end
end

如果上述选项不太适合您的使用案例,您可以查看have_enqueued_job matcher以获取更多选项。

相关问题