问题
I'm trying out email_spec, which says it supports Pony, but I'm not sure how I'd go about testing emails in a sinatra app. The examples in the readme show usages with rails ActionMailer
, but not in Pony.
Not precious about using email_spec, so any other ideas for testing emails using rspec in sinatra are welcomed =)
回答1:
I ended up looking at the pony spec file, and stealing code from it to write my specs =)
This is what I have:
./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:
I haven't used email_spec before, but it looks pretty cool.
You can see how I test pony in the spec file, which may work for your case:
https://github.com/benprew/pony/blob/master/spec/pony_spec.rb
Also, you can deliver messages via :test, and it will use the test mailer included with mail:
See "Using Mail with Testing or Spec'ing Libraries" at https://github.com/mikel/mail for an example of examining the test messages.
来源:https://stackoverflow.com/questions/8504101/how-do-i-test-pony-emailing-in-a-sinatra-app-using-rspec