如何解决在节点中使用JEST的模拟类
所以,这实际上是我发生过几次的事情,我不知道如何解决。
我有这样的课程
//myClass.js
const { get } = require('../models/myModel');
class MyClass {
constructor(id) {
this.id = id;
}
async getByID() {
return get(this.id);
}
}
和一个控制器
//controller.js
module.exports = MyClass;
const MyClass = require('./MyClass.js');
module.exports = {
getController: (req,res,next) => {
try {
const MyObject = new MyClass(1);
const info = await MyObject.getByID();
res.send(info)
}
catch (e) {
next(e);
}
}
}
当我想进行E2E测试时,我需要模拟getByID
来查看其解决和拒绝时发生的情况。
那么,如何模拟构造函数?我做了这样的事情
// myclass.spec.js
const MyClass = require('./MyClass.js');
jest.mock('./MyClass',() => jest.fn().mockImplementation(() => ({
getByID: Promise.resolve({id: 1,name: 'John'}),})));
describe('Testing MyClass',() => {
it('Should return info',() => {
const res = httpMocks.createResponse();
const mReq = httpMocks.createRequest();
const mNext = jest.fn();
const mRes = mockResponse();
await pipelineController(mReq,mRes,mNext);
expect(mRes.send).toHaveBeenCalledWith(1);
done();
})
})
我知道此测试当前有效,但是现在我无法更改getByID
模拟值以查看拒绝承诺时会发生什么。
如果我尝试将其包含在测试(it
)中....它将不会模拟任何内容...
我想要类似的东西
const MyClass = require('./MyClass.js');
const {pipelineController} = require('./controllers.js')
jest.mock('./MyClass',() => jest.fn().mockImplementation(() => ({
getInfo: jest.fn(),() => {
ProcessService.getInfo.mockImplementation(() => Promise.resolve('the value i want'))
const res = httpMocks.createResponse();
const mReq = httpMocks.createRequest();
const mNext = jest.fn();
const mRes = mockResponse();
await pipelineController(mReq,mNext);
expect(mRes.send).toHaveBeenCalledWith(1);
done();
})
it('Should return info',() => {
ProcessService.getInfo.mockImplementation(() => Promise.reject('the reason i want'))
const res = httpMocks.createResponse();
const mReq = httpMocks.createRequest();
const mNext = jest.fn();
const mRes = mockResponse();
await pipelineController(mReq,mNext);
expect(mRes.send).toHaveBeenCalledWith(1);
done();
})
})
解决方法
由于getByID
在实例化后立即被调用,因此应该在模拟类之外提供间谍以更改实现。
这可以通过works in obscure and unspecified ways类自动模拟来完成,但可能适用于这种特定情况:
jest.mock('./MyClass.js')
const MyClass = require('./MyClass.js');
...
MyClass.prototype.getByID.mockResolvedValue({id: 1,name: 'John'});
// instantiate MyClass and call getByID
这可以通过在班外暴露间谍来完成:
const mockGetByID = jest.fn();
jest.mock('../../../services/ProcessService',() => jest.fn(() => (
{ getByID: mockGetByID }
)));
const MyClass = require('./MyClass.js');
...
mockGetByID.mockResolvedValue({id: 1,name: 'John'});
// instantiate MyClass and call getByID
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。