如何使用rspec在Sinatra应用程序中测试Pony电子邮件?

时间:2011-12-14 12:00:07

标签: rspec sinatra pony email-spec

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

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

2 个答案:

答案 0 :(得分:3)

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

这就是我所拥有的:

./规格/ 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

./规格/集成/ 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

答案 1 :(得分:2)

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

你可以在spec文件中看到我如何测试小马,这可能适合你的情况:

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

此外,您可以通过:test传递消息,它将使用mail:

中包含的测试邮件程序

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

相关问题