【问题标题】:Rspec - Testing an instance of a methodRspec - 测试方法的实例
【发布时间】:2021-12-12 08:16:26
【问题描述】:

我实现了一个条件,即如果用户的电子邮件未得到确认,他们就不会收到电子邮件。我的逻辑是,如果电子邮件未经过验证/确认,则返回ActionMailer::Base::NullMail.new 的实例。

我写了这个测试

 if @user.verified?
  next
 else 
  ActionMailer::Base::NullMail.new
 end 

 expect(TestMailer.test_mail(expired_account)).to be_an_instance_of(ActionMailer::Base::NullMail)

我得到的错误是

expected #<ActionMailer::MessageDelivery(#<ActionMailer::Base::NullMail:0x00007fb0f2f24090>)> to be a kind of ActionMailer::Base::NullMail

问题是,我怎样才能重新编写我的测试,以便我可以测试如果电子邮件未经过验证,它们应该返回ActionMailer::Base::NullMail.new 的实例

【问题讨论】:

  • 我认为你必须使用expect(TestMailer.test_mail(expired_account).message).to be.....,你需要使用message方法获取实际的电子邮件对象

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


【解决方案1】:

错误是告诉你问题是对的吗?您需要一个邮件类型对象的实例,因此将其更改为此。此外,您应该能够在Rspec see docs 中使用be_a 方法代替be_an_instance_of

expect(TestMailer.test_mail(expired_account).message)
  .to be_an(ActionMailer::Base::NullMail)

【讨论】:

  • 谢谢!刚刚熟悉测试,所以这对我来说非常具有挑战性。
  • @LindaKadz 当然,这有点棘手,因为.message 可能不是最冗长的名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-05
  • 2022-09-28
  • 1970-01-01
  • 2013-01-31
相关资源
最近更新 更多