WebMock Stub_request 与 hash_include -- 仅匹配 json 的一部分

发布于 2025-01-10 14:26:56 字数 655 浏览 4 评论 0原文

我想测试请求是否包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文