I have Sinatra app which sends email on post request:
post '/test_mailer' do
Pony.mail(
to: 'me@mine.com.au',
from: 'me@mine.com.au',
subject: 'Howdy!',
body: erb(:body) )
end
So I want to test this behaviour using:
require 'spec_helper'
describe 'App' do
before(:each) do
Pony.stub!(:deliver)
end
it "sends mail" do
Pony.should_receive(:mail) do |mail|
mail.to.should == [ 'joe@example.com' ]
mail.from.should == [ 'sender@example.com' ]
mail.subject.should == 'hi'
mail.body.should == 'Hello, Joe.'
end
Pony.mail(to: 'joe@example.com', from: 'sender@example.com', subject: 'hi', body: 'Hello, Joe.')
end
it 'test_mailer' do
Pony.should_receive(:mail) do |mail|
mail.to.should == ['me@mine.com.au']
end
post '/test_mailer'
end
end
This is my spec_helper
require File.join(File.dirname(__FILE__), '..', 'app.rb')
require 'sinatra'
require 'rack/test'
# setup test environment
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false
def app
Sinatra::Application
end
RSpec.configure do |config|
config.include Rack::Test::Methods
end
But I receive error:
mailer(master)» rspec spec/app_spec.rb
.F
Failures:
1) App test_mailer
Failure/Error: Pony.should_receive(:mail) do |mail|
(Pony).deliver(any args)
expected: 1 time
received: 0 times
# ./spec/app_spec.rb:20:in `block (2 levels) in <top (required)>'
Finished in 0.02542 seconds
2 examples, 1 failure
Failed examples:
rspec ./spec/app_spec.rb:19 # App test_mailer
So, how should I properly test that post '/test_mailer'
request?
Maybe I'm being dumb, but it looks quite obvious - there's nothing in your production code which calls a deliver
method. Shouldn't you be verifying Pony.should_receive(:mail)
?
Update: I see Pony has a private class method called deliver, but you're stubbing that, so it will never be called.