RSpec:如何期望两个不同对象上的有序消息?

时间:2014-07-10 13:25:42

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

如何预期两个不同对象上的有序消息?目前我有这样的事情:

call_order = ''

expect(double1).to receive(:msg1) { call_order += '1' }
expect(double2).to receive(:msg2) { call_order += '2' }

expect(call_order).to eq '12'

我知道我可以使用以下命令在单个对象上预期订阅消息:

expect(double).to receive(:msg).ordered
expect(double).to receive(:other_msg).ordered

1 个答案:

答案 0 :(得分:3)

根据doc,rspec 3允许

collaborator_1 = double("Collaborator 1")
collaborator_2 = double("Collaborator 2")

expect(collaborator_1).to receive(:step_1).ordered
expect(collaborator_2).to receive(:step_2).ordered
expect(collaborator_1).to receive(:step_3).ordered

collaborator_1.step_1
collaborator_2.step_2
collaborator_1.step_3

#=> the examples should all pass