gpt4 book ai didi

ruby - 使用 rspec 如何测试以正确顺序接收散列参数的方法?

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:30 24 4
gpt4 key购买 nike

不幸的是,我需要与 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 调用以正确的顺序接收消息哈希?

最佳答案

于是在接下来的google中我找到了答案。 should_receive 可以占用一个 block ,所以我可以重建我的测试

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

现在,当 key 被弄乱时,我的测试将按预期中断,我可以花更多的时间来解决其他人脆弱的代码...

关于ruby - 使用 rspec 如何测试以正确顺序接收散列参数的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19898582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com