SuperTest / JEST ,如何强制或模拟 400 状态码?
SuperTest / JEST , how to force or mock a 400 status code?
我正在使用 Jest 和 SuperTest 进行单元测试,但我想不出一种方法来强制路由抛出错误。
我附上了我的代码片段:
const router = require('express').Router();
const Thoughts = require('../models/thought.model')
router.get('/thoughts', (req,res)=>{
Thoughts.find().then((thought)=>{
res.status(200).send({thought})
}).catch((err)=>{
res.sendStatus(400).send(err)
})
})
const normRoutes = require('../routes/normRoutes')
const request = require('supertest');
const express = require('express');
const mongoose = require('mongoose')
const app = express();
app.use('/api', normRoutes)
beforeAll(async () => {
const url = "MONGO_URL"
await mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true })
})
test('GET /api/thoughts --> 400 if error', () => {
## missing logic to force a 400
request(app)
.get('/api/thoughts)
.expect(400)
.done()
})
模拟 Thought
模型的 find
方法以在您的测试中出现错误而拒绝。
首先,在您的测试中导入模型并使用 jest 自动模拟它。
const normRoutes = require('../routes/normRoutes')
const request = require('supertest');
const express = require('express');
const mongoose = require('mongoose');
const Thoughts = require('../models/thought.model');
# This is hoisted so the previous import it a jest mock.
jest.mock('../models/thought.model');
接下来,通过在 automock 上调用 mockRestore
,确保测试在 运行 每次测试之前使用实际实现。
const app = express();
app.use('/api', normRoutes)
beforeAll(async () => {
const url = "MONGO_URL"
await mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true })
})
beforeEach(() => {
# This restores the mock back to the actual Thoughts model implementation.
# This allows for other tests to use the actual implementation.
# https://jestjs.io/docs/mock-function-api#mockfnmockrestore
Thoughts.mockRestore();
});
最后,在测试中mock find 的实现应该报错。
test('GET /api/thoughts --> 400 if error', () => {
# Thought model find method is mocked to reject with an error.
Thoughts.mockImplementation(() => ({
find: jest.fn().mockRejectedValueOnce(
new Error('Connection Failed or something')
);
}));
## missing logic to force a 400
request(app)
.get('/api/thoughts')
.expect(400)
.done();
})
我正在使用 Jest 和 SuperTest 进行单元测试,但我想不出一种方法来强制路由抛出错误。
我附上了我的代码片段:
const router = require('express').Router();
const Thoughts = require('../models/thought.model')
router.get('/thoughts', (req,res)=>{
Thoughts.find().then((thought)=>{
res.status(200).send({thought})
}).catch((err)=>{
res.sendStatus(400).send(err)
})
})
const normRoutes = require('../routes/normRoutes')
const request = require('supertest');
const express = require('express');
const mongoose = require('mongoose')
const app = express();
app.use('/api', normRoutes)
beforeAll(async () => {
const url = "MONGO_URL"
await mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true })
})
test('GET /api/thoughts --> 400 if error', () => {
## missing logic to force a 400
request(app)
.get('/api/thoughts)
.expect(400)
.done()
})
模拟 Thought
模型的 find
方法以在您的测试中出现错误而拒绝。
首先,在您的测试中导入模型并使用 jest 自动模拟它。
const normRoutes = require('../routes/normRoutes')
const request = require('supertest');
const express = require('express');
const mongoose = require('mongoose');
const Thoughts = require('../models/thought.model');
# This is hoisted so the previous import it a jest mock.
jest.mock('../models/thought.model');
接下来,通过在 automock 上调用 mockRestore
,确保测试在 运行 每次测试之前使用实际实现。
const app = express();
app.use('/api', normRoutes)
beforeAll(async () => {
const url = "MONGO_URL"
await mongoose.connect(url, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true })
})
beforeEach(() => {
# This restores the mock back to the actual Thoughts model implementation.
# This allows for other tests to use the actual implementation.
# https://jestjs.io/docs/mock-function-api#mockfnmockrestore
Thoughts.mockRestore();
});
最后,在测试中mock find 的实现应该报错。
test('GET /api/thoughts --> 400 if error', () => {
# Thought model find method is mocked to reject with an error.
Thoughts.mockImplementation(() => ({
find: jest.fn().mockRejectedValueOnce(
new Error('Connection Failed or something')
);
}));
## missing logic to force a 400
request(app)
.get('/api/thoughts')
.expect(400)
.done();
})