ruby Rails:测试ActiveMailer传递方法设置

yqlxgs2m  于 2023-08-04  发布在  Ruby
关注(0)|答案(1)|浏览(115)

我想测试我的UserMailer是否正在向客户(在代码中称为租户)指定的正确SMTP服务器发送消息。
在用户邮件中使用before_action钩子:
该钩子的代码为:

191   def set_smtp_settings
192     tenant_email_provider = @user.tenant.email_providers.where(provider_type: 'smtp').take
193     tenant_smtp_settings = (tenant_email_provider&.settings || {}).with_indifferent_access
194     
195     current_settings = (message.delivery_method.settings || {} ).with_indifferent_access
196     new_settings = current_settings.merge(tenant_smtp_settings)
197     
198     raise "No server address to send to" if new_settings[:address].nil?
199     
200     message.delivery_method.settings = new_settings
201   end

字符串
单元测试为:

9   test 'that the settings are according to a mailer' do
 10     expected_settings = email_provider.settings.with_indifferent_access
 11 
 12     message = @mailer.account_activation_confirm
 13     sent_message = message.deliver_now
 14 
 15     actual_settings = sent_message.delivery_method.settings.with_indifferent_access
 16 
 17     intersecting_keys = expected_settings.keys.map(&:to_sym) | actual_settings.keys
 18     assert intersecting_keys.include?(:address)
 19 
 20     intersecting_keys.each do |setting|
 21       expected = expected_settings[setting].to_s
 22       actual = actual_settings[setting].to_s
 23       assert_equal(expected, actual, "smtp value of setting '#{setting}' do not match (#{expected} != #{actual})")
 24     end


该测试的结果是:

Failure:
DeliverSettingsTest#test_that_the_settings_are_according_to_a_mailer [/opt/xxx-web/test/mailers/deliver_settings_test.rb:25]:
smtp value of setting 'address' do not match (mail.hello.com != ).
--- expected
+++ actual
@@ -1 +1,3 @@
-"mail.hello.com"
+# encoding: US-ASCII
+#    valid: true
+""


此外,在单元测试的第15行,sent_message.delivery_method.settings.with_indifferent_access是一个空哈希。而在钩子的行200中,设置了message.delivery_method.settings
添加调试器调用和打印输出作为证明:
在钩子中:

200|     message.delivery_method.settings = new_settings
=> 201|     debugger
   202|     nil
   203|   end
   204| 
   205| end
=>#0    UserMailer#set_smtp_settings at /opt/xxx-web/app/mailers/user_mailer.rb:201
  #1    block {|target=#<UserMailer:0x00000000018998>, value=nil, block=nil|} in make_lambda at /usr/local/bundle/gems/activesupport-7.0.5.1/lib/active_support/callbacks.rb:400
  # and 53 frames (use `bt' command for all frames)
(ruby) message.delivery_method.settings
{"address"=>"mail.hello.com",                      <----- Populated Hash
 "port"=>25,
 "domain"=>"localhost.localdomain",
 "user_name"=>"prius",
 "password"=>"bunch_tr1k3s!",
 "authentication"=>nil,
 "enable_starttls"=>nil,
 "enable_starttls_auto"=>true,
 "openssl_verify_mode"=>nil,
 "ssl"=>nil,
 "tls"=>nil,
 "open_timeout"=>5,
 "read_timeout"=>5}
(rdbg)


在测试环境中

(rdbg) c    # continue command
[12, 21] in /opt/xxx-web/test/mailers/deliver_settings_test.rb
    12|     message = @mailer.account_activation_confirm
    13|     sent_message = message.deliver_now
    14| 
    15|     actual_settings = sent_message.delivery_method.settings.with_indifferent_access
    16| 
=>  17|     debugger
    18| 
    19|     intersecting_keys = expected_settings.keys.map(&:to_sym) | actual_settings.keys
    20|     assert intersecting_keys.include?(:address)
    21| 
=>#0    block in <class:DeliverSettingsTest> at /opt/xxx-web/test/mailers/deliver_settings_test.rb:17
  #1    block in run (3 levels) at /usr/local/bundle/gems/minitest-5.18.1/lib/minitest/test.rb:94
  # and 24 frames (use `bt' command for all frames)
(ruby) actual_settings
{}                                             <--------- Empty Hash


ActiveMailer库或测试存根是否在发送后清除delivery_method.settings?

cig3rfwq

cig3rfwq1#

根据Rails指南,使用after_action而不是before_action
“使用after_action回调还可以通过更新mail.delivery_method.settings覆盖传递方法设置。”
--https://guides.rubyonrails.org/action_mailer_basics.html#action-mailer-callbacks

相关问题