在 cuke 断言中包含或忽略未知变量

发布于 2024-11-05 19:38:11 字数 541 浏览 0 评论 0原文

您好,我有一个 Cuke 测试,正在测试带有 json 的 REST Web 服务。它看起来像这样:

When "joe" posts the following to "comments" as "application/json":
    """
    {
      "name": "Pop! Pop!",
      "body": "party over here yo!"
    }
    """
Then the status code returned should be 201
And the Location header returned should be .*\/comments\/\d+
And the json returned should be
    """
    {
      "id": 40563
      "name": "Pop! Pop!",
      "body": "party over here yo!"
    }
    """

问题是我不知道的 Id,因为它是一个自动递增的数据库 Id。有没有办法让断言忽略该元素?

Greetings, I have a Cuke test that is testing a rest web service w/ json. it looks something like this:

When "joe" posts the following to "comments" as "application/json":
    """
    {
      "name": "Pop! Pop!",
      "body": "party over here yo!"
    }
    """
Then the status code returned should be 201
And the Location header returned should be .*\/comments\/\d+
And the json returned should be
    """
    {
      "id": 40563
      "name": "Pop! Pop!",
      "body": "party over here yo!"
    }
    """

The problem is the Id which I will not know because it's a auto incremented datebase Id. Is there a way I can tel the assert to ignore that element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九公里浅绿 2024-11-12 19:38:11

这有点令人讨厌,但您可以修改 JSON 字符串中的 ID,使其每次都相同。

json_response.gsub!(/id: \d*/, 'id: 999')

也许最好将期望重写为如下所示:

And the JSON should contain these attributes:
  | id   | <integer>           |
  | name | Pop! Pop!           |
  | body | party over here yo! |

It's a bit of a nasty hack, but you could modify the ID in the JSON string to make it the same every time.

json_response.gsub!(/id: \d*/, 'id: 999')

Perhaps it would better to rewrite the expectation as something like this:

And the JSON should contain these attributes:
  | id   | <integer>           |
  | name | Pop! Pop!           |
  | body | party over here yo! |
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文