不幸的是我需要与Soap API进行交互。如果这还不够糟糕,那么API会启用参数排序,这意味着,无论XML是什么,它都必须以正确的元素顺序构建。
我正在使用Savon所以我正在构建一个有序的哈希。然而,在一些重构之后,真正的调用停止了工作,而我的所有测试都继续通过。典型的测试如下:
it 'should receive correctly ordered hash' do
example_id = 12345789
our_api = Example::ApiGateway.new()
params = {:message=>{'ApiKey' => api_key, 'ExampleId' => example_id}}
Savon::Client.any_instance.should_receive(:call).with(:get_user_details, params).and_return(mocked_response())
our_api.get_user(example_id: example_id)
end
哈希比较完全不关心键的顺序,因此无论收到的实际哈希顺序如何,此测试都会通过。我想只是抓住call
方法接收的参数,然后我可以比较每个哈希的有序键,但我不知道如何做到这一点。
如何确保Savon调用以正确的顺序接收消息哈希?
答案 0 :(得分:2)
所以在接下来的谷歌我找到了答案。 should_receive
可以阻止,所以我可以重建我的测试
it 'should receive correctly ordered hash' do
example_id = 12345789
our_api = Example::ApiGateway.new()
params = {:message=>{'ApiKey' => api_key, 'ExampleId' => example_id}}
Savon::Client.any_instance.should_receive(:call){ |arg1, arg2|
arg1.should eq(:get_user_details)
#Ensure order here manually
arg2[:message].keys.should eq(params[:message].keys)
mocked_response()
}
our_api.get_user(example_id: example_id)
end
现在我的测试会在按键被搞乱时按预期打破,我可以花更多时间来解决其他人的脆弱代码......