gpt4 book ai didi

javascript - 如何使用 jest 函数发送响应?它给出了异步回调错误

转载 作者:行者123 更新时间:2023-12-02 23:04:50 25 4
gpt4 key购买 nike

我尝试使用 Jest 来模拟模型响应。它将进入我的 jest.mock.createMondayBox 函数,并且可以打印“hello world”,这意味着它可以真正进入替代原始模型的模拟模型。

模型/mondayModel.js

var mondayModel = function() {
function createBox (req, payload, callback) {
...
boxClient.request(req, {
method: 'POST'
path:
body:
headers: { }
}, function(error, response) {
if(error || (response.statusCode && response.statusCode !== 200)) {
return callback(new ErrorObject({
errorName:'createBoxFailure',
errorMessage: 'Error in creating box'
})
}
return callback(null, {resultSet: response.body})
})

}
function fnB (req, callback) { }
function fnC (req, callback) { }
function fnD (req, callback) { }
return {
createBox: createBox,
fnB: fnB,
fnC: fnC,
fnD: fnD
}
}

module.exports = mondayModel

Controller /boxController.js

var MondayModel = require('../models/mondayModel');


function createMondayBox(req, res, next) {
...
var mondayModel = new MondayModel();
mondayModel.createBox(req, payload, function(error, result) {

if(error) {
res.json({'status': 'BADREQUEST', 'statusCode': 400})

} else {
var mondaybox = result.resultSet.mondayboxes && result.resultSet.mondayboxes[0]

var mappedResponse = Response.mapCreateMondaybox(req, resultSet);
utils.logKK(req, utils.getKK(mappedResponse.mondayboxes[0]))
res.json(mappedResponse);
}

...
}
}

boxController-test.jest


let boxController = null

describe('money tracker', () => {
beforeAll(() => {
jest.mock('../../../../models/mondayBox',
() => function mondayBoxModel() {
console.log("hello world")
return {
createBox: (req, payload, callback) => {
return callback(null, {
resultSet: {
mondayboxes: [{ name: '1' }, { name: '2' }]
},
json: jest.fn()
})}
fnB: jest.fn(),
fnC: jest.fn(),
fnD: jest.fn()
}
}
}))
)
boxController = require('../controllers/boxController')
})

test('success case', done => {
const req = {}
const res = new Promise(r =>
r({ json: jest.fn(), status: jest.fn() })
)
mondayBoxController.createMondayBox(req, res, (cbErr, cbRes) => {
expect(res.json).toBeCalled();
done()
})
}
}

类型错误:res.json 不是函数

我可以知道如何解决这个问题吗?如何编写模拟模型响应?谢谢

最佳答案

首先,您只需要测试 Controller ,因此您应该这样做

将 Controller 更新为

var MondayModel = require('../models/mondayModel');


function createMondayBox(req, res, next) {
...
var mondayModel = new MondayModel();
mondayModel.createBox(req, payload, function(error, result) {

if(error) {
res.json({'status': 'BADREQUEST', 'statusCode': 400})

} else {
var mondaybox = result.resultSet.mondayboxes &&
result.resultSet.mondayboxes[0]

var mappedResponse = Response.mapCreateMondaybox(req, resultSet);
utils.logKK(req, utils.getKK(mappedResponse.mondayboxes[0]))
res.json(mappedResponse);
next();
}

...
}
}

在测试文件中:

import {createRequest, createResponse}  from 'node-mocks-http'
import boxController from 'controller/boxController.js'
import mondayBoxModal from 'models/mondayBox';

jest.mock('models/mondayBox', () => () => ({ createBox: (req, payload, cb) => cb(null, { resultSet: { mondayboxes: ['resultsData']} } }));

describe('money tracker', () => {
let req;
let res;
let next = jest.fn;
beforeEach(() => {
jest.clearAllMocks();
req = createRequest({
method: 'GET',
url: '/user/42',
params: {
id: 42
}
});

res = httpMocks.createResponse();
res.json = jest.fn;

})

test('success case', done => {
const req = {}

mondayBoxController.createMondayBox(req, res, (cbErr, cbRes) => {
expect(res.json).toBeCalled();
done()
})
}

}

关于javascript - 如何使用 jest 函数发送响应?它给出了异步回调错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57636883/

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