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

我有一个用户注册function测试。 如何测试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消息那样正常工作。

我究竟做错了什么?

编辑

function规范如下所示:

 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 

由于您正在执行高级function规范,因此我要下注,因为单击'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匹配器以获取更多选项。