【问题标题】:Invalid chai property jsonschema无效的柴属性 jsonschema
【发布时间】:2021-09-27 17:11:04
【问题描述】:

我正在使用 mocha 和 chai 进行 API 自动化。

我需要比较api的响应,并与chai jsonschema断言进行比较。

expect(response).to.be.jsonSchema(expectedResponse)

我收到以下错误,

 Error: Invalid Chai property: jsonSchema
      at Object.proxyGetter [as get] (node_modules\chai\lib\chai\utils\proxify.js:78:17)
      at _callee2$ (test\/ServerEndPointsTest.js:70:21)
      at tryCatch (node_modules\regenerator-runtime\runtime.js:63:40)
      at Generator.invoke [as _invoke] (node_modules\regenerator-runtime\runtime.js:294:22)
      at Generator.next (node_modules\regenerator-runtime\runtime.js:119:21)
      at asyncGeneratorStep (node_modules\@babel\runtime\helpers\asyncToGenerator.js:3:24)
      at _next (node_modules\@babel\runtime\helpers\asyncToGenerator.js:25:9)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)

【问题讨论】:

  • 你使用哪个 chai 插件?
  • 我的 package.json 中的 Chai 插件 "chai": "^4.3.4", "chai-json-schema": "^1.5.1",
  • 你是如何使用chai-json-schema的?展示最小的、可重现的代码示例
  • response = res.body expect(response).to.be.jsonSchema(expectedResponse.GetCCAccounts.with) ExpectedResponse.js var expectedResponse = { GetCCAccounts: { "with":{ "response": { "code": '0303', "desc": '无效的会话令牌。' }, "httpCode": 400 }

标签: mocha.js chai web-api-testing


【解决方案1】:

你没有提到你的测试文件的导入,很可能那是你犯错的地方。

当您尝试使用 chai-json-schema 插件未内置的东西时,chai 库会抛出这些错误。

尝试如下更新您的导入:

const {expect} = require("chai").use(require('chai-json-schema'));

这会将 chai 插件所需的方法添加到您的期望对象中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多