【问题标题】:Trying to test sending of email in rSpec but ActionMailer::Base.deliveries.empty? is true尝试在 rSpec 中测试发送电子邮件但 ActionMailer::Base.deliveries.empty?是真的
【发布时间】:2011-12-19 03:33:30
【问题描述】:

我不知道为什么它不起作用。经过数小时试图弄清楚这一点后,我编写了一个小测试来检查 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 => "foobar@gmail.com"

        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

【问题讨论】:

  • 在我的spec_helper.rb 文件中,我之前必须明确设置ActionMailer::Base.delivery_method = :test;你试过这个吗?
  • 你在 spec_helper 文件的什么地方添加了这个?我刚刚尝试将它添加到 RSpec.configure do |config|阻止,它没有任何效果。
  • 我把它放在config 块之外。我记得有很多问题,我不能 100% 确定这就是解决问题的原因。
  • 不走运,我已经玩了好几个小时了。表单可以正常工作,但希望我的测试能够正常工作
  • 那么,您的测试不应该读取ActionMailer::Base.deliveries.empty?.should be_false 以符合您的代码吗?

标签: ruby-on-rails ruby-on-rails-3 rspec ruby-on-rails-3.1 rspec2


【解决方案1】:

刚刚发现了一个用 rspec 测试电子邮件的好方法:https://github.com/bmabey/email-spec

希望它会有所帮助。

【讨论】:

    【解决方案2】:

    我似乎总是做这样的傻事:

    因为我手动创建了 passwords_spec 文件,而不是使用 rspec 生成器,所以我忘记在文件顶部添加“require 'spec_helper'”。生成器会自动完成的事情。

    最近几天我一直在找出我的愚蠢错误。有趣的是,当我变得懒惰并决定先构建我的应用程序然后进行测试时,这一切是如何发生的。好吧,我已经从我的错误中吸取了教训。先测试!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-13
      • 1970-01-01
      • 2011-01-05
      • 2017-10-27
      • 1970-01-01
      • 2016-10-11
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多