首页 > 解决方案 > Chai-http [POST] 不允许在 bodyParameters 上发送数组(API 测试和 Javascript)

问题描述

我在我的测试用例中发现了这种奇怪的行为。

让我们重新创建场景

我确实有 2 节课:

API.ts -> 指的是我创建请求的类。

API-op.ts -> 指的是我通过断言、期望等进行验证的类。

上下文:方法之间存在一些依赖关系。我的意思是,如果我想发布,我首先需要一个 ID,该 ID 来自一个名为 postCreateId 的方法。获得响应正文后,我将使用该 ID 传递给我的 postPublishArt 方法

所以。

我在 API.ts 上有这个方法

public postPublishArt(token: any, Id: any) {
    return chai.request(URLBASE)
        .post(URI)
        .set({ Authorization: "Bearer " + token })
        .send({
            id: Id
        })
}

因此,我在 API-op.ts 上调用该方法,如下所示:

public async postPublish() {
        var accessToken = undefined;
        return this.chaiClass.generateToken()
            .then(async (response) => {
                accessToken = response.body.AccessToken;
                return this.chaiArt.postCreateId(accessToken)
                    .then(async (response) => {
                        let id = response.body.id;
                        let array: Array<string> = [id];
                        return this.chaiArt.postPublishArt(accessToken, array)
                            .then(async (response) => {
                                expect(response).to.have.status(201);
                                return response;
                            }).catch(err => {
                                return console.log('Failure', err);
                            });
                    });
            });
    }

在 Postman 上,bodyparameter 需要是这样的:

{
      "ids": 
          [
"d2164919-730a-5fe2-924d-5c14cf36e38c"
]
      
}

注意方括号,因为 body 参数要求 ids 键必须是数组。

错误是什么:就像我只需要一个 id 作为数组传递一样,一旦我转换了该变量并将其发送到 POST 方法。POST 方法显示此错误:

text:'{"errors":[{"code":"unexpected-exception","details":{"statusCode":500,"error":"invalid input syntax for type timestamp: \\"0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN\\""}}]}'

我认为当您使用 ARRAY 发送 POST 但只传递一个参数时,问题与 chai-http 有关。

让我知道我是否错了

标签: javascripttypescriptpostchaichai-http

解决方案


回答:有一个问题是sugerAgent没有回调send方法......

请参阅下面报告的问题及其解决方法:https ://github.com/chaijs/chai-http/issues/290


推荐阅读