gpt4 book ai didi

reactjs - 如何编写测试用例以通过 mocha nyc for API 传递代码覆盖率?

转载 作者:行者123 更新时间:2023-12-03 17:11:57 25 4
gpt4 key购买 nike

我正在尝试使用 mocha 编写测试用例来测试 Strapi API 我尝试在文档中搜索但无法理解它我只是想知道我们究竟如何编写用于测试任何 API 的单元测试用例。早些时候我使用下面显示的方法,但在使用 **nycIstanbul 包进行代码覆盖后,它显示 0% 的行和许多分支。

const request = require("co-supertest");
var assert = require("chai").assert;

const { SERVER_URL, PAYLOAD } = require("../config/config");
let JWT;
let dataId;

describe("States Module Endpoint", function () {
before(function (done) {
request(SERVER_URL)
.post("/auth/local")
.send(PAYLOAD)
.expect(200)
.expect("Content-Type", /json/)
.end(function (err, res) {
if (err) return done(err);
const response = res.body;
JWT = response["jwt"];
done();
});
});

describe("Create Method", function () {
// case for empty,required and correct params for Create method done here
describe("POST /crm-plugin/states/", function () {
it("should not create an entry when empty params test case is executed", function (done) {
request(SERVER_URL)
.post("/crm-plugin/states")
.send({})
.set("Authorization", "Bearer " + JWT)
.expect(200)
.end(function (err, res) {
if (err) return done(err);
assert.isEmpty(
res.body,
"Empty response is expected when params are empty"
);
done();
});
});

it("should not create an entry when required params test case is executed", function (done) {
request(SERVER_URL)
.post("/crm-plugin/states")
.send({
is_active: true,
})
.set("Authorization", "Bearer " + JWT)
.expect(200)
.end(function (err, res) {
if (err) return done(err);
assert.isEmpty(
res.body,
"Empty response is expected when required params are missing"
);
done();
});
});

it("should create an entry when correct params test case is executed", function (done) {
request(SERVER_URL)
.post("/crm-plugin/states")
.send({
name: "Gujarat",
})
.set("Authorization", "Bearer " + JWT)
.expect(200)
.end(function (err, res) {
dataId = res.body.id;
if (err) return done(err);
assert.strictEqual(
res.body.name,
"Gujarat",
"Object in response should not differ"
);
dataId = res.body.id;
done();
});
});
});
});

describe("Update Method", function () {
// case for correct params done for update method
describe("PUT /crm-plugin/states/:id", function () {
it("should update the data when correct params test case is executed", function (done) {
request(SERVER_URL)
.put("/crm-plugin/states/" + dataId)
.send({
name: "Goa",
})
.set("Authorization", "Bearer " + JWT)
.expect(200)
.end(function (err, res) {
if (err) return done(err);
assert.strictEqual(
res.body.name,
"Goa",
"Object in response should not differ"
);
done();
});
});
});
});

describe("Find Method", function () {
// case for empty params done here
describe("GET /crm-plugin/states", function () {
it("responds with all records when empty params test case is executed", function (done) {
request(SERVER_URL)
.get("/crm-plugin/states")
.set("Authorization", "Bearer " + JWT)
.expect(200)
.end(function (err, res) {
if (err) return done(err);
assert.isAtLeast(
res.body.length,
1,
"Find method should return atleast one response."
);
done();
});
});
});
});

describe("FindOne Method", function () {
// case for correct params done here
describe("GET /crm-plugin/states/:id", function () {
it("responds with matching records when correct params test case is executed", function (done) {
request(SERVER_URL)
.get("/crm-plugin/states/" + dataId)
.set("Authorization", "Bearer " + JWT)
.expect(200)
.end(function (err, res) {
if (err) return done(err);
assert.strictEqual(
res.body.name,
"Goa",
"FindOne Method should return response with same name"
);
done();
});
});
});
});

describe("Count Method", function () {
// case for count done here
describe("GET /crm-plugin/states/count", function () {
it("should return data count when correct params test case is executed", function (done) {
request(SERVER_URL)
.get("/crm-plugin/states/count")
.set("Authorization", "Bearer " + JWT)
.expect(200)
.end(function (err, res) {
if (err) return done(err);
assert.isAtLeast(res.body, 1, "Count expected to be atleast 1");
done();
});
});
});
});

describe("Delete Method", function () {
// case for correct params done here
describe("DELETE /crm-plugin/states/:id", function () {
it("should delete entry when correct params test case is executed", function (done) {
request(SERVER_URL)
.delete("/crm-plugin/states/" + dataId)
.set("Authorization", "Bearer " + JWT)
.expect(200)
.end(function (err, res) {
if (err) return done(err);
assert.strictEqual(
res.body.name,
"Goa",
"Object in response should not differ"
);
done();
});
});
});
});

});

最佳答案

我认为本指南会对您有所帮助 - https://github.com/strapi/strapi/pull/6324

这是您为 Strapi 应用程序创建测试套件的方法。

关于reactjs - 如何编写测试用例以通过 mocha nyc for API 传递代码覆盖率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61978313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com