为了使我的测试套件更具可读性,我在我的规范中引入了间谍,但我不确定如何处理类方法。有可能"窥探一个班级"?
我们说我有以下示例代码
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
是否可以使用间谍重写规范?
由于
答案 0 :(得分:4)
您可以通过allow
加expect
使用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