0

有一个 api blueprint dredd 测试问题困扰了我一段时间。

我可以dredd测试json格式成功,但是对于文本格式,它总是失败。

The fail message -> fail: body: Real and expected data does not match. 

但是我检查的预期和真实的身体是一样的。

任何人都可以帮助我或给我一些建议吗?我很感激。

以下是我的代码:

### Get counting camera [GET]

+ Response 200 (text/plain)

+ Body

      vadp_module_number='3'
      vadp_module_order='1,2,0'

以下是dredd测试结果:

fail: body: Real and expected data does not match.

expected:
headers:
Content-Type: text/plain

body:
vadp_module_number='3'
vadp_module_order='1,2,0'

statusCode: 200


actual:

statusCode: 200
headers:
    date: Thu, 14 Jun 2018 14:09:39 GMT
    server: Boa/0.94.14rc21
    accept-ranges: bytes
    connection: close
    content-type: text/plain
    cache-control: no-cache
    pragma: no-cache
    content-length: 4856

body:
vadp_module_number='3'
vadp_module_order='1,2,0'
4

2 回答 2

0

结合 API 蓝图,Dredd 可以在带有文本/纯文本的尾随换行符上失败。见apiaryio/dredd#67

有关解决方法,请参阅Dredd 文档中的删除预期纯文本正文中的尾随换行符部分:

var hooks = require('hooks');

hooks.beforeEach(function(transaction) {
  if (transaction.expected.headers['Content-Type'] === 'text/plain') {
    transaction.expected.body = transaction.expected.body.replace(/^\s+|\s+$/g, "");
  }
});
于 2019-03-25T13:28:45.597 回答
0

似乎你的身体没有正确缩进。

相机.md

FORMAT: 1A

HOST: http://127.0.0.1:5000

# Camera

The camera is a simple API allowing consumers to manage your security cameras.

## Camera [/camera]

### Get counting camera [GET]

+ Response 200 (text/plain; charset=utf-8)

    + Body

        vadp_module_number='3'
        vadp_module_order='1,2,0'

我用 Drakov 模拟服务器和 Dredd 对其进行了测试,它运行良好。

德拉科夫

drakov -f ./camera.md --public --watch --discover

或者使用 docker 运行它

德瑞德

dreed.yml(片段)

...
hooks-worker-handler-port: 61321
config: doc/examples/dredd.yml
blueprint: doc/examples/camera.md
endpoint: 'http://localhost:5000'

并运行:

dredd --config ./dredd.yml

或者也使用 docker 运行dredd

结果是

info: Configuration './dredd.yml' found, ignoring other arguments.
2018-06-25T17:30:23.883Z - warn: Parser warning in file 'camera.md': message-body asset is expected to be a pre-formatted code block, every of its line indented by exactly 12 spaces or 3 tabs on lines 17-18
2018-06-25T17:30:23.884Z - info: Beginning Dredd testing...
2018-06-25T17:30:23.920Z - pass: GET (200) /camera duration: 34ms
2018-06-25T17:30:23.920Z - complete: 1 passing, 0 failing, 0 errors, 0 skipped, 1 total
2018-06-25T17:30:23.921Z - complete: Tests took 36ms
于 2018-06-25T17:51:03.213 回答