【问题标题】:Testing mailer 'from' header using rspec使用 rspec 测试邮件“发件人”标头
【发布时间】:2014-04-24 03:55:04
【问题描述】:

我正在开发一个 ruby​​ on rails 项目,我正在尝试测试 'from' 标头字段,但测试用例失败。

这是我在邮件操作中设置的内容

def some_actoin
  mail(to: xyz@example.com,
         from: '"Example" <service@example.com>',
         subject: "test subject")
  end
end

在 rspec 测试用例中,

mail.from.should == '"Example" <service@example.com>'

我的测试用例因以下错误而失败

expected: '"Example" <service@example.com>'
got: [service@example.com] (using ==)

用发件人电子邮件地址测试发件人标题是不是错误的方法?

感谢任何帮助!

【问题讨论】:

  • 礼貌是接受一个好的回答

标签: ruby-on-rails testing actionmailer rspec-rails


【解决方案1】:

mail.from 仅包含发件人电子邮件地址。您还需要测试显示名称。

mail.from.should eq(['service@example.com'])
mail[:from].display_names.should eq(['Example'])

【讨论】:

  • 山姆,感谢您的快速回复。我想我们将能够通过使用'mail gem'来实现上述目标。有没有办法在不使用“邮件宝石”的情况下测试显示名称?
  • 似乎display_names 将返回 UTF-8 的编码(?)值,所以我不能用于测试。相反,您可以使用mail[:from].value.(至少对于 Rails 5)
猜你喜欢
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
  • 2014-05-10
  • 2011-04-07
  • 2020-06-13
相关资源
最近更新 更多