MERN 单元测试失败
MERN unit test fails
我正在尝试开发 MERN 应用程序。但是 MERN POST 方法单元测试失败并出现以下错误
错误:
1 失败
- POST/product/save/
好的,创建一个新产品有效:
AssertionError: 预期 { error: { errors: { …(5) }, …(3) } } 有 属性 '_id'
在上下文中。 (test/api/product/post.js:33:29)
在 processTicksAndRejections(节点:internal/process/task_queues:96:5
我附上了下面的代码。
MERNProject/products.js
const mongoose = require("mongoose");
const postSchema = new mongoose.Schema({
productName: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
price: {
type: String,
required: true,
},
productCategory: {
type: String,
required: true,
},
productUrl: {
type: String,
required: true,
},
});
module.exports = mongoose.model("Products", postSchema);
MERNProject/routes/products.js
const express = require("express");
const Products = require("../models/products");
const router = express();
//save Products
router.post("/product/save", (req, res) => {
let newProduct = new Products(req.body);
newProduct.save((err) => {
if (err) {
return res.status(400).json({
error: err,
});
}
return res.status(200).json({
success: "Product saved successfuly",
});
});
});
module.exports = router;
MERNProject/test/api/post.js
const expect = require("chai").expect;
const request = require("supertest");
const app = require("../../../routes/products.js");
const conn = require("../../../server.js");
describe("POST /product/save/", () => {
before((done) => {
conn
.connect()
.then(() => done())
.catch((err) => done(err));
});
after((done) => {
conn
.close()
.then(() => done())
.catch((err) => done(err));
});
it("OK, creating a new product works", async () => {
const res = await request(app).post("/product/save/").send({
productName: "Mango",
description: "description Mango",
price: "rs 50",
productCategory: "fruits",
productUrl:
"https://upload.wikimedia.org/wikipedia/commons/9/90/Hapus_Mango.jpg",
});
const body = res.body;
expect(body).to.contain.property("_id");
expect(body).to.contain.property("productName");
expect(body).to.contain.property("description");
expect(body).to.contain.property("price");
expect(body).to.contain.property("productCategory");
expect(body).to.contain.property("productUrl");
});
});
MERNProject/server.js
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
//import routes
const postRoutes = require("./routes/products");
//app middleware
app.use(bodyParser.json());
app.use(cors());
app.use(postRoutes);
const PORT = 8000;
const DB_URL =
"mongodb+srv://twg:twg123@cluster0.ong7q.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
function connect() {
return new Promise((resolve, reject) => {
mongoose
.connect(DB_URL)
.then((res, err) => {
if (err) return reject(err);
//console.log(`DB connected`);
resolve();
})
.catch((err) => console.log(`DB connection error`, err));
});
}
function close() {
return mongoose.disconnect();
}
connect();
app.listen(PORT, () => {
console.log(`App is running on ${PORT}`);
});
module.exports = { connect, close };
您的 post 请求可能返回了一个错误,并且 body
包含一个错误而不是您期望的对象。从痕迹上看,好像是这样的:
{ error: { errors: { …(5) }, …(3) } }
我会说你的测试工作正常,断言引发错误并且测试失败:)
要进一步调查,您应该记录 res
的内容
我正在尝试开发 MERN 应用程序。但是 MERN POST 方法单元测试失败并出现以下错误
错误:
1 失败
- POST/product/save/ 好的,创建一个新产品有效: AssertionError: 预期 { error: { errors: { …(5) }, …(3) } } 有 属性 '_id' 在上下文中。 (test/api/product/post.js:33:29) 在 processTicksAndRejections(节点:internal/process/task_queues:96:5
我附上了下面的代码。
MERNProject/products.js
const mongoose = require("mongoose");
const postSchema = new mongoose.Schema({
productName: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
price: {
type: String,
required: true,
},
productCategory: {
type: String,
required: true,
},
productUrl: {
type: String,
required: true,
},
});
module.exports = mongoose.model("Products", postSchema);
MERNProject/routes/products.js
const express = require("express");
const Products = require("../models/products");
const router = express();
//save Products
router.post("/product/save", (req, res) => {
let newProduct = new Products(req.body);
newProduct.save((err) => {
if (err) {
return res.status(400).json({
error: err,
});
}
return res.status(200).json({
success: "Product saved successfuly",
});
});
});
module.exports = router;
MERNProject/test/api/post.js
const expect = require("chai").expect;
const request = require("supertest");
const app = require("../../../routes/products.js");
const conn = require("../../../server.js");
describe("POST /product/save/", () => {
before((done) => {
conn
.connect()
.then(() => done())
.catch((err) => done(err));
});
after((done) => {
conn
.close()
.then(() => done())
.catch((err) => done(err));
});
it("OK, creating a new product works", async () => {
const res = await request(app).post("/product/save/").send({
productName: "Mango",
description: "description Mango",
price: "rs 50",
productCategory: "fruits",
productUrl:
"https://upload.wikimedia.org/wikipedia/commons/9/90/Hapus_Mango.jpg",
});
const body = res.body;
expect(body).to.contain.property("_id");
expect(body).to.contain.property("productName");
expect(body).to.contain.property("description");
expect(body).to.contain.property("price");
expect(body).to.contain.property("productCategory");
expect(body).to.contain.property("productUrl");
});
});
MERNProject/server.js
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const cors = require("cors");
const app = express();
//import routes
const postRoutes = require("./routes/products");
//app middleware
app.use(bodyParser.json());
app.use(cors());
app.use(postRoutes);
const PORT = 8000;
const DB_URL =
"mongodb+srv://twg:twg123@cluster0.ong7q.mongodb.net/myFirstDatabase?retryWrites=true&w=majority";
function connect() {
return new Promise((resolve, reject) => {
mongoose
.connect(DB_URL)
.then((res, err) => {
if (err) return reject(err);
//console.log(`DB connected`);
resolve();
})
.catch((err) => console.log(`DB connection error`, err));
});
}
function close() {
return mongoose.disconnect();
}
connect();
app.listen(PORT, () => {
console.log(`App is running on ${PORT}`);
});
module.exports = { connect, close };
您的 post 请求可能返回了一个错误,并且 body
包含一个错误而不是您期望的对象。从痕迹上看,好像是这样的:
{ error: { errors: { …(5) }, …(3) } }
我会说你的测试工作正常,断言引发错误并且测试失败:)
要进一步调查,您应该记录 res