Rspec rubocop错误“更喜欢使用验证双打而不是普通双打”

我已经进行了RSpec测试

let(:mail_instance) { double(deliver_later: nil) }

before do
  allow(Confirmationmailer).to receive(:send_email).and_return(mail_instance)
  allow(mail_instance).to receive(:deliver_later)
end

it 'calls mailer,and delivers the mail' do
  call_endpoint
  expect(mail_instance).to have_received(:deliver_later)
end

它运行良好,但出现了rubocop错误-prefer using verifying doubles over normal doubles。根据文档https://www.rubydoc.info/gems/rubocop-rspec/1.7.0/RuboCop/Cop/RSpec/VerifiedDoubles,我应该改用instance_double,但如果更改它,我的规格会出错:

  

ArgumentError:          预期使用模块或字符串,得到{:deliver_later => nil}

zhang4xue1 回答:Rspec rubocop错误“更喜欢使用验证双打而不是普通双打”

您应该使用instance_double的想法,但是我想了解Rubocop为什么这么说很重要。

doublesinstance_doubles之间的主要区别是 如果我们尝试声明一个不是由模拟实例的类实现的方法,它将引发异常。如果我们决定在测试中使用模拟对象,则与使用间谍或常规双打相比,instance_doubles使我们对测试更有信心。

因此,如果将来有人重命名或删除该方法,则如果您使用double,则可能永远不知道并且您会通过,但是如果您使用instance_doubles,则会收到通知。

在两者之间可能会有一点性能折衷,但是在我99%的时间中,最好使用instance_doubles

,

您需要将您正在存根的课程传递给instance_double

let(:mail_instance) { instance_double(ActionMailer::MessageDelivery,deliver_later: nil) }
本文链接:https://www.f2er.com/3153509.html

大家都在问