【发布时间】:2017-03-29 15:26:03
【问题描述】:
当使用 mandril-api 时,Rails Action Mailer 会被绕过,所以不可能做这样的事情
it 'sends an email' do
expect { subject.send_instructions }
.to change { ActionMailer::Base.deliveries.count }.by(1)
end
我正在尝试使用 object_double 来测试我的邮件。我要测试的正是发送到 API 的参数(通过选项哈希)。
到目前为止,我这里有 Mandrill 代码
MANDRILL.messages.send_template( options[:template], [], message) unless Rails.env.staging?
MANDRILL 只是与 API 的连接,如下所述。
describe 'verify content' do
it 'uses the correct template' do
api = object_double(Mandrill::API.new(ENV['MANDRILL_KEY']).messages)
allow(api).to receive(:send_template)
PostNotificationMailer.format_options(participant, post)
expect(api).to have_received(:send_template)
#expect(options[:template]).to eq('crowdai_standard_template')
end
end
我希望能够在此处检查传递给 Mandrill API 的所有参数。我可以模拟 messages 方法,但不能模拟 messages.send_template
1) PostNotificationMailer verify content uses the correct template
Failure/Error: expect(api).to have_received(:send_template)
(ObjectDouble(#<Mandrill::Messages:0x007f8debd4f348 @master=#<Mandrill::API:0x007f8debd4faf0 @host="https://mandrillapp.com", @path="/api/1.0/", @session=#<Excon::Connection:7f8debd4f758 @data={:chunk_size=>1048576, :ciphers=>"HIGH:!SSLv2:!aNULL:!eNULL:!3DES", :connect_timeout=>60, :debug_request=>false, :debug_response=>false, :headers=>{"User-Agent"=>"excon/0.51.0"}, :idempotent=>false, :instrumentor_name=>"excon", :middlewares=>[Excon::Middleware::Hijack, Excon::Middleware::ResponseParser, Excon::Middleware::Expects, Excon::Middleware::Idempotent, Excon::Middleware::Instrumentor, Excon::Middleware::Mock], :mock=>false, :nonblock=>true, :omit_default_port=>false, :persistent=>false, :read_timeout=>60, :retry_limit=>4, :ssl_verify_peer=>true, :ssl_uri_schemes=>["https"], :stubs=>:global, :tcp_nodelay=>false, :thread_safe_sockets=>true, :uri_parser=>URI, :versions=>"excon/0.51.0 (x86_64-darwin15) ruby/2.3.1", :write_timeout=>60, :host=>"mandrillapp.com", :hostname=>"mandrillapp.com", :path=>"", :port=>443, :query=>nil, :scheme=>"https"} @socket_key="https://mandrillapp.com:443">, @debug=false, @apikey="redacted">>) (anonymous)).send_template(*(any args))
expected: 1 time with any arguments
received: 0 times with any arguments
# ./spec/mailers/post_notification_mailer_spec.rb:14:in `block (3 levels) in <top (required)>'
** 编辑 **
有一个 gem MandrillMailer 解决了针对 Mandril API 进行测试的问题,但是它的构建被破坏了,而且它似乎也在内部重新构建 API。
How do I test mandrill api with rspec
我找不到任何关于如何使用 object_double 的教程或清晰的示例。
【问题讨论】:
-
这实际上有两部分:如何在测试期间拦截发送到 Mandrill API 的选项哈希,以及如何在 Rspec 中使用 object_double
-
请在主题中粘贴
subject.send_instructions内容方法
标签: ruby-on-rails rspec mandrill