我试图为这个函数编写一个使用jestJS的测试,但我遇到了一些问题,如何模拟socket.emit()和getPwmDutyCycle()函数:
module.exports = async (app, socket) => {
const res = []
const array = ['some', 'example', 'elements']
array.map((elm, index) => {
res.push(app.locals['target' + (index + 1)].getPwmDutyCycle())
})
socket.emit('status', res)
}这就是我想出来的:
const status = require('../lib/status.js')
test('should emit dc values', () => {
const app = {
locals: {
target1: 1,
target2: 2,
target3: 3
}
}
const socket = { emit: jest.fn() }
status(app, socket)
expect(socket.emit).toHaveBeenCalled()
expect(socket.emit.mock.calls[0][0]).toBe('status')
expect(socket.emit.mock.calls[1][0]).toBe([1, 2, 3])
})发布于 2018-10-16 08:38:50
您的app存根错过了getPwmDutyCycle方法:
const app = {
locals: {
target1: { getPwmDutyCycle: () => 1},
target2: { getPwmDutyCycle: () => 2},
target3: { getPwmDutyCycle: () => 3}
}
}https://stackoverflow.com/questions/52831059
复制相似问题