尝试在 rSpec 中测试发送电子邮件,但 ActionMailer::Base.deliveries.empty?是真的
我不知道为什么它不起作用。经过几个小时的尝试解决这个问题后,我编写了一个小测试来检查 ActionMailer::Base.deliveries 是否为空,确实如此。
当我测试重置表单时,它可以工作并且发送邮件,但是当我运行测试时,它似乎没有在数组中存储任何内容。
require 'spec_helper'
describe "Passwords" do
describe "reset password" do
it "emails user when requesting password reset" do
visit login_path
click_link "Forgot Password?"
response.should render_template "passwords/new"
response.should have_selector :h1, :content => "Reset Password"
fill_in "password_reset[email]", :with => "[email protected]"
click_button "Reset Password"
response.should render_template "users/new"
response.should have_selector :div, :id => "flash_notice", :content => "Email sent with password reset instructions."
ActionMailer::Base.deliveries.empty?.should be_true
# mail = ActionMailer::Base.deliveries.last
end
end
end
I have no idea why it's not working. After hours of trying to figure this out I wrote a small test to check if the ActionMailer::Base.deliveries was empty and it was.
When I test my reset form it works and mail is sent but when I run test it doesn't seem to store anything in the array.
require 'spec_helper'
describe "Passwords" do
describe "reset password" do
it "emails user when requesting password reset" do
visit login_path
click_link "Forgot Password?"
response.should render_template "passwords/new"
response.should have_selector :h1, :content => "Reset Password"
fill_in "password_reset[email]", :with => "[email protected]"
click_button "Reset Password"
response.should render_template "users/new"
response.should have_selector :div, :id => "flash_notice", :content => "Email sent with password reset instructions."
ActionMailer::Base.deliveries.empty?.should be_true
# mail = ActionMailer::Base.deliveries.last
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
刚刚发现了一个很棒的宝石来使用 rspec 测试电子邮件: https://github.com/bmabey/email-spec
希望它会有所帮助。
Just found out a great gem to test emails with rspec: https://github.com/bmabey/email-spec
Hope it will help.
我似乎总是做这样的愚蠢事情:
因为我手动创建了passwords_spec 文件而不是使用rspec 生成器,所以我忘记在文件顶部添加“require 'spec_helper'”。生成器会自动完成的事情。
这几天我一直在反思自己犯下的愚蠢错误。有趣的是,当我变得懒惰并决定先构建我的应用程序然后再进行测试时,这一切是如何发生的。好吧,我已经从错误中吸取了教训。先测试一下!
I always seem to do silly things like this:
Because I created the passwords_spec file manually and not using the rspec generator I forgot to add "require 'spec_helper'" at the top of the file. Something that the generator would have done automatically.
The last few days I have been figuring out my silly mistakes. Funny how all this happened when I got lazy and decided to build my app first then test after. Well I've learnt from my mistake. Test first!