简体   繁体   English

Chai-http [POST] 不允许在 bodyParameters 上发送数组(API 测试和 Javascript)

[英]Chai-http [POST] does not allow to send an Array on bodyParameters (API Testing & Javascript)

I have found this kind of weird behavior in my test case.我在我的测试用例中发现了这种奇怪的行为。

Let's recreate the scenario让我们重新创建场景

I do have 2 classes:我确实有 2 节课:

API.ts -> which refers to the class where I'm creating the requests. API.ts -> 指的是我在其中创建请求的 class。

API-op.ts -> which refers to the class where I'm validating through asserts, expects etc. API-op.ts -> 指的是 class,我通过断言、期望等进行验证。

Context: There are some dependencies between the methods.上下文:方法之间存在一些依赖关系。 I mean, If I want to publish I need first an ID, that ID becomes from a method called postCreateId.我的意思是,如果我想发布,我首先需要一个 ID,该 ID 来自一个名为 postCreateId 的方法。 Once I have the response body, I'm using that ID to be passed on my postPublishArt method获得响应正文后,我将使用该 ID 传递给我的 postPublishArt 方法

So.所以。

I have this method on API.ts我在 API.ts 上有这个方法

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

So, I'm calling that method on API-op.ts like this:因此,我在 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); }); }); }); }

on Postman the bodyparameter needs to be like this:在 Postman 上,bodyparameter 需要是这样的:

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

Pay attention at square brackets because the body parameter requires that ids keys MUST BE array.注意方括号,因为 body 参数要求 ids 键必须是数组。

What is the error: Like I need just ONE id passed as an array, once I convert that variable and it is sent to the POST method.错误是什么:就像我只需要一个 id 作为数组传递一样,一旦我转换了该变量并将其发送到 POST 方法。 the POST method is showing up this error: POST 方法显示此错误:

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

I think that issue is about chai-http when you send a POST with ARRAY but just passing one parameter.我认为当您使用 ARRAY 发送 POST 但只传递一个参数时,问题与 chai-http 有关。

Let me know if I'm wrong让我知道我是否错了

ANSWER: There is an issue that sugerAgent is not calling back the send method...回答:有一个问题是sugerAgent没有回调send方法......

see below the issue reported and how it is solved: https://github.com/chaijs/chai-http/issues/290请参阅下面报告的问题及其解决方法: https://github.com/chaijs/chai-http/issues/290

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM