我可以窥探一堂课吗?

时间:2016-11-16 08:03:17

标签: ruby rspec

为了使我的测试套件更具可读性,我在我的规范中引入了间谍,但我不确定如何处理类方法。有可能"窥探一个班级"?

我们说我有以下示例代码

  def publish(post)
    Publisher.call(post)
    post.save
  end

和通讯规范

it 'delegates the publishing to Publisher' do
  let(:blog) { ... }
  let(:post) { ... }

  expect(Publisher).to receive(:call).with(post).and_call_original
  blog.publish(post)
end

是否可以使用间谍重写规范?

由于

1 个答案:

答案 0 :(得分:4)

您可以通过allowexpect使用spies on partial doubles

it 'delegates the publishing to Publisher' do
  let(:blog) { ... }
  let(:post) { ... }

  allow(Publisher).to receive(:call)
  blog.publish(post)
  expect(Publisher).to have_received(:call).with(post)
end