双重和间谍有什么区别?

时间:2016-07-15 21:24:41

标签: ruby rspec

我正在阅读关于spies的内容,我看不出它之间存在差异:

  File "/usr/lib/python2.7/dist-packages/django/forms/models.py", line 294, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (is_admin) specified for Freelancer

而且:

RSpec.describe "have_received" do
  it "passes when the message has been received" do
    invitation = spy('invitation', deliver: "ok")
    invitation.deliver
    expect(invitation).to have_received(:deliver)
  end
end

两个例子都没有问题。我使用RSpec.describe "have_received" do it "passes when the message has been received" do invitation = double('invitation', deliver: "ok") invitation.deliver expect(invitation).to have_received(:deliver) end end

看不出任何优势

1 个答案:

答案 0 :(得分:0)

正如@max在评论中所说,区别在于我可以使用spy而不说deliver: "ok"。所以,运行这个:

RSpec.describe "have_received" do
  it "passes when the message has been received" do
    invitation = spy('invitation')
    invitation.deliver
    expect(invitation).to have_received(:deliver)
  end
end

RSpec.describe "have_received" do
  it "passes when the message has been received" do
    invitation = double('invitation')
    invitation.deliver
    expect(invitation).to have_received(:deliver)
  end
end

第一个例子将通过,但不是第二个。

enter image description here

现在我明白了这意味着......

“你可以使用任何测试双重(或部分双重)作为间谍,但必须设置双重监视你关心的消息。间谍​​自动监视所有消息,或者你可以允许消息窥探它。“