Node.js 如何测试在节点中调用另一个外部API的API
我正在使用mocha、chai和chai http测试我的简单API,该API将调用从Slack路由到Habitica,集成了这两个服务 我试图从创建测试开始,但我面临这个问题:当我调用我的API时,代码在外部API调用之前返回。这是测试代码:Node.js 如何测试在节点中调用另一个外部API的API,node.js,mocha.js,chai,chai-http,Node.js,Mocha.js,Chai,Chai Http,我正在使用mocha、chai和chai http测试我的简单API,该API将调用从Slack路由到Habitica,集成了这两个服务 我试图从创建测试开始,但我面临这个问题:当我调用我的API时,代码在外部API调用之前返回。这是测试代码: var chai = require("chai"); var chaiHttp = require("chai-http"); var server = require("../src/app/index"); var should = chai.sh
var chai = require("chai");
var chaiHttp = require("chai-http");
var server = require("../src/app/index");
var should = chai.should();
chai.use(chaiHttp);
describe("/GET list", () => {
it("it should return a list of user\'s tasks", (done) => {
chai.request(server)
.post("/habitica")
.type("urlencoded")
.send({text: "list"})
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a("object");
res.body.should.have.property("success").eql("true");
done();
});
});
});
app.post("/habitica", server.urlencodedParser, function(req, res) {
if (typeof req.body !== "undefined" && req.body) {
switch(req.body.text) {
case "list":
request({
url: GET_TASKS,
headers: { "x-api-user": process.env.HABITICA_USERID, "x-api-key": process.env.HABITICA_APITOKEN }
}, function (apiError, apiResponse, apiBody) {
if (apiError) {
res.send(apiError);
} else {
res.send(apiBody);
}
});
break;
default:
res.send({
"success": "false",
"message": "Still working on tasks creation"
});
}
}
});
这是测试调用的代码:
var chai = require("chai");
var chaiHttp = require("chai-http");
var server = require("../src/app/index");
var should = chai.should();
chai.use(chaiHttp);
describe("/GET list", () => {
it("it should return a list of user\'s tasks", (done) => {
chai.request(server)
.post("/habitica")
.type("urlencoded")
.send({text: "list"})
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a("object");
res.body.should.have.property("success").eql("true");
done();
});
});
});
app.post("/habitica", server.urlencodedParser, function(req, res) {
if (typeof req.body !== "undefined" && req.body) {
switch(req.body.text) {
case "list":
request({
url: GET_TASKS,
headers: { "x-api-user": process.env.HABITICA_USERID, "x-api-key": process.env.HABITICA_APITOKEN }
}, function (apiError, apiResponse, apiBody) {
if (apiError) {
res.send(apiError);
} else {
res.send(apiBody);
}
});
break;
default:
res.send({
"success": "false",
"message": "Still working on tasks creation"
});
}
}
});
此代码在调用Habitica返回任何值之前返回。这是“npm测试”的结果:
我已经在很多论坛和网站上搜索过:
- :这很有道理,但既然它只是一个简单的集成服务,我应该测试什么呢
- 有人说我应该模拟外部api结果:但我不会测试任何东西,因为它只是一个集成
提前感谢。您应该模拟对外部API的调用,并在调用外部API失败或成功后测试应用程序的行为。 您可以按如下方式测试不同的场景
describe("/GET list", () => {
// pass req.body.text = 'list'
describe("when task list is requested", () => {
describe("when task list fetched successfully", () => {
// in beforeEach mock call to external API and return task list
it('returns tasks list in response', () => {
})
}),
describe("when error occurs while fetching task list", () => {
// in beforeEach mock call to external API and return error
it('returns error in response', () => {
})
})
}),
// when req.body.text != 'list'
describe("when task list is not requested", () => {
it('returns error in response', () => {
})
})
})
那么您的应用程序中有一个端点/惯习,对吗?或者它是第三方服务?是的,我的应用程序中有这个端点。此终结点调用第三方服务。@AndréMaldona问题解决了吗?如果是,如何解决?