【问题标题】:How do I test Pony emailing in a Sinatra app, using rspec?如何使用 rspec 在 Sinatra 应用程序中测试 Pony 电子邮件?
【发布时间】:2012-01-20 04:55:23
【问题描述】:

我正在试用email_spec,它说它支持 Pony,但我不确定如何在 sinatra 应用程序中测试电子邮件。自述文件中的示例显示了 rails ActionMailer 的用法,但在 Pony 中没有。

使用 email_spec 并不珍贵,因此欢迎任何其他在 sinatra 中使用 rspec 测试电子邮件的想法 =)

【问题讨论】:

    标签: rspec sinatra pony email-spec


    【解决方案1】:

    我最终查看了pony spec file,并从中窃取了代码来编写我的规范 =)

    这就是我所拥有的:

    ./spec/spec_helper.rb

    def do_not_send_email
      Pony.stub!(:deliver)  # Hijack deliver method to not send email
    end
    
    RSpec.configure do |conf|
      conf.include Rack::Test::Methods
      conf.mock_with :rspec
    
      # ...
    
      conf.before(:each) do
        do_not_send_email
      end
    end
    

    ./spec/integration/invite_user_spec.rb

    require_relative '../spec_helper'
    
    feature "Invite user" do
      scenario "should send an invitation email" do
        visit "/"
        click_link "start-btn"
    
        within_fieldset("Invite new user") do
          fill_in 'new_user_email', :with => 'franz@gmail.com'
    
          Pony.should_receive(:mail) { |params|
            params[:to].should == "franz@gmail.com"
            params[:subject].should include("You are invited to blah")
    
            params[:body].should include("You've been invited to blah")
            params[:body].should include("/#{@account_id}/new-user/register")
          }
          click_button 'new_user'
        end
    
        page.should have_content("Invitation email has been sent")
      end
    end
    

    【讨论】:

      【解决方案2】:

      我之前没用过 email_spec,但它看起来很酷。

      您可以在规范文件中查看我如何测试小马,这可能适用于您的情况:

      https://github.com/benprew/pony/blob/master/spec/pony_spec.rb

      此外,您可以通过 :test 传递消息,它将使用邮件中包含的测试邮件:

      有关检查测试消息的示例,请参阅https://github.com/mikel/mail 上的“将邮件用于测试或指定库”。

      【讨论】:

      • 这就是我所做的——从你的规范文件中窃取代码;)
      • 这个答案(来自 gem 的维护者)应该是公认的答案,因为不需要模拟和存根。为了完整起见,这是您在测试中停止实时邮件传递的方式; Pony.override_options = { via: :test }。然后可以通过Mail::TestMailer.deliveries.first 访问“已发送”邮件。其余的,包括 RSpec 匹配器在邮件 gem 的 README 中介绍github.com/mikel/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多