简体   繁体   English

Api在邮递员中传递,但在mocha js中失败

[英]Api passes in postman but it fails in mocha js

i have some API and if i test them with postman its working fine. 我有一些API,如果我用邮递员测试它的工作正常。 but if i try to test that API with same data in mocha js, some API are working and some are giving me an error like "500 internal server error" and "400 bad request". 但是,如果我尝试在mocha js中使用相同的数据测试该API,则某些API正在运行,有些正在给我一个错误,如“500内部服务器错误”和“400错误请求”。

i am sure that i am passing same data, same request and same authorization details in both postman and in mocha test script. 我确信我在邮递员和摩卡测试脚本中传递相同的数据,相同的请求和相同的授权详情。

i have tried this in mocha and i am passing same data in postman, than why mocha giving me and error like this. 我在摩卡尝试了这个,我在邮递员中传递相同的数据,而不是为什么摩卡给我和这样的错误。 my all server and database are up and working fine. 我的所有服务器和数据库都运行良好。

it("Updating User details: ", function (done) {
        server
            .put(apiUrl + "/user")
            .set({
                "name": "Vinay Pandya",
                "photo": "http://tamilcinema.com/wp-content/uploads/2015/05/madhavan.jpg",
                "email": "mail@mail.com",
                "gender": "M"
            })
            .set({"Authorization": token})
            .expect(200)
            .end(function (err, res) {
                if (err)
                    throw err;
                res.status.should.equal(200);
                res.body.status.should.equal("success");
                //console.info(JSON.stringify(res.body));
            });
        done();
    });

You should put the done call inside of the asynchronous request. 您应该将done调用放在异步请求中。 So to modify your example code: 所以要修改你的示例代码:

it("Updating User details: ", function (done) {
    server
        .put(apiUrl + "/user")
        .set({
            "name": "Vinay Pandya",
            "photo": "http://tamilcinema.com/wp-content/uploads/2015/05/madhavan.jpg",
            "email": "mail@mail.com",
            "gender": "M"
        })
        .set({"Authorization": token})
        .expect(200)
        .end(function (err, res) {
            if (err)
                throw err;
            res.status.should.equal(200);
            res.body.status.should.equal("success");
            //console.info(JSON.stringify(res.body));
            done();
        });
});

i solved this issue. 我解决了这个问题。 i was using SET() instead of SEND(). 我使用SET()而不是SEND()。 if i want to send data, than i should use SEND(). 如果我想发送数据,我应该使用SEND()。 SET() is for setting header for api request. SET()用于设置api请求的标头。

it("Updating User details: ", function (done) {
this.timeout(300);
        server
            .put(apiUrl + "/user")
            .send({
                "name": "Vinay Pandya",
                "photo": "http://tamilcinema.com/wp-content/uploads/2015/05/madhavan.jpg",
                "email": "mail@mail.com",
                "gender": "M"
            })
            .set({"Authorization": token})
            .expect(200)
            .end(function (err, res) {
                if (err)
                    throw err;
                res.status.should.equal(200);
                res.body.status.should.equal("success");
                //console.info(JSON.stringify(res.body));
                done();
            });

    });

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

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