reactjs - 如何编写测试用例以使用 mocha nyc 为 API 传递代码覆盖率?
问题描述
我正在尝试使用 mocha 编写一个测试用例来测试strapi API 我尝试在文档中搜索但无法理解它我只是想知道我们究竟如何编写一个单元测试用例来测试任何 API。早些时候我使用下面显示的方法,但在使用 **nyc Istanbul 包进行代码覆盖后,它显示行和许多分支的 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 应用程序创建测试套件的方法。
推荐阅读
- python-3.x - 为什么递归比python3中的迭代慢?
- jquery - 使用 Jquery UI 排序除前三个单元格外的表行排序
- count - Tableau:CountD OR Bins 的计数与条件
- python - 如何将此事件转换为适当的状态命令,当它添加到公会时循环其状态以更新?不和谐.py
- mysql - MySQL 行丢失但得到 .MYD 文件
- chart.js - 即使添加新数据集,如何保持条形大小相同
- c - 为什么这个 c 程序中没有通过测试用例?
- here-api - 调用“https://wse.ls.hereapi.com/2/findsequence.json”时出现“TooManyRequests”错误。这里-api
- powerapps - 如何在powerapps公式栏中链接两个公式?
- wordpress - Aavance 自定义字段无法隐藏我的页面内容编辑器