WebMock Stub_request 与 hash_include -- 仅匹配 json 的一部分
我想测试请求是否包含 json 中的某些内部值。假设请求正文是:
level1: {
level2a: "value1",
level2b: "value2",
...
},
...
}
然后我有以下代码:
WebMock.stub_request(:post, "http://127.0.0.1:/path")
.with(body: hash_including({level1: {level2a: 'value1'}}))
.to_return(status: 200, body: '{}', headers: {})
不幸的是,上面的代码不起作用。我一直希望它能仅匹配 json 的子集。
根据文档:匹配一个哈希包括指定的键或键/值对。忽略任何附加键。我不确定它是否按我想要的方式工作。
但这里的要求太大了,无法全部引用。
对于为什么测试不起作用以及如何修复或重写它有什么想法吗?
I wold like to test whether a request contained some inner value in json. Let assume the request body is:
level1: {
level2a: "value1",
level2b: "value2",
...
},
...
}
Then I have the following code:
WebMock.stub_request(:post, "http://127.0.0.1:/path")
.with(body: hash_including({level1: {level2a: 'value1'}}))
.to_return(status: 200, body: '{}', headers: {})
The code above does not work unfortunately. I've been hoping it'll work with matching only sub-set of json.
According to the doc: Matches a hash that includes the specified key(s) or key/value pairs. Ignores any additional keys. I am not sure if it works the way I want.
The request is too big here though for quoting it all.
Any ideas for why the test is not working and how to fix or rewrite it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论