Leo*_*Teo 8 rspec ruby-on-rails
我写了一个RSpec集成测试.根据test.log,我可以看到它已经发送了一封电子邮件,但是当我尝试使用ActionMailer :: Base.deliveries访问该电子邮件时,它总是显示它是空的.
我已经阅读了其他类似的问题,并尝试了一切.我很难过.
以下是使用Capybara/RSpec的代码:
it "should notify owner" do
puts "Delivery method: #{ActionMailer::Base.delivery_method.inspect}"
# This returns: ":test", which is correct
# Get other guests to do the review
@review = Review.last
share_link = "http://#{@account.subdomain}.cozimo.local:#{Capybara.server_port}/review/#{@review.slug}"
visit(share_link)
fill_in "email", :with => @user1.email
fill_in "password", :with => "foobar"
click_button "Start Review"
# Add a comment
click_on "ice-global-note-button"
find(:css, "#ice-global-note-panel > textarea.ui-corner-all").set "Foo"
click_on "ice-global-note-submit-button"
# Test is failing here. WTF? The array should not be empty
ActionMailer::Base.deliveries.empty?.should be_false
# Check that a notification email was sent to the owner
open_email(@owner.email)
current_email.should have_content "Hi #{@owner.first_name}"
end
Run Code Online (Sandbox Code Playgroud)
如上所示,config.action_mailer.delivery_method =:test
在test.log中,它显示电子邮件确实已发送!
Sent mail to somebody1@example.com (62ms)
Date: Tue, 29 Jan 2013 13:53:48 -0500
From: Review Studio <support@cozimo.com>
To: somebody1@example.com
Message-ID: <51081abcd9fbf_5bd23fef87b264a08066@Leonards-MacBook-Pro.local.mail>
Subject: Notes added for Test
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Hi Bruce,
Just letting you know that you have new notes added to the review:
Project: Test
Description: Test description
URL: http://ballistiq.cozimo.local:3000/review/625682740
Thanks,
Review Studio
Completed 200 OK in 170ms (Views: 0.2ms | ActiveRecord: 4.3ms)
Run Code Online (Sandbox Code Playgroud)
这是使用Capybara和Selenium的集成测试.因此,您必须等待应用程序实际发送邮件,然后再检查它是否已发送邮件.
添加一个sleep 1告诉rspec在触发发送邮件事件后等待.然后通过检查ActionMailer :: Base.deliveries数组并传递它来恢复.
如上所述,这通常是不好的做法,因为它会减慢测试速度.
集成测试不应该测试邮件是否完全发送.测试应分为明确的测试课程职责.因此,我们将以不同的方式构建测试,以便我们仅测试在另一个类(控制器或资源测试)中发送的邮件.我们还可以使用期望来检查是否实际调用了mail方法,尽管我们仍然可能会遇到时间问题.
你永远不应该在规格上使用睡眠或那些耗时的方法,我只会减慢你的规格!
尝试在邮件中使用期望值,在测试开始时添加类似的内容
mail = mock(mail)
mail.should_receive(:deliver)
YourMailer.should_receive(:your_method).once.and_return(mail)
Run Code Online (Sandbox Code Playgroud)
这样你就不必等待,你实际上是在测试你必须测试的内容(代码创建和传递邮件)而不是邮件程序代码(你只在邮件对象上调用传递,实际传递是一项工作的 ActionMailer 测试并且您在您的应用程序上与它无关,您应该相信调用这些方法有效)
| 归档时间: |
|
| 查看次数: |
3201 次 |
| 最近记录: |