gpt4 book ai didi

node.js - 如何用 Jest 模拟函数 .save() sequelize

转载 作者:行者123 更新时间:2023-12-03 22:16:30 30 4
gpt4 key购买 nike

我想用 jest 模拟 sequelize 中的 .save() 函数。但是我在文件 user.services.test.js 中发现我的代码有错误。有没有人可以帮助我?

The error said : result.save() is not a function.

user.services.test.js

 it('Should update user that input by ', async () => {

let result;
let data ;
let dataInput = {id : 1, username : "teguh", userPassword : "apake"};
user.findByPk = jest.fn(() => {
return {username : "teguh", userPassword : "iyah", save : mockFuntion()};
});
data = user.findByPk();
data.userPassword = dataInput.userPassword;
data.save = jest.fn(() => {return {saved:true}});

result = await service.updateUser(dataInput);
expect(data.save).toBeCalledTimes(1);
expect(data.userPassword).toEqual(dataInput.userPassword);
});

user.services.js

async updateUser(user){
let result;
try {
const pass = bcrypt.hashSync(user.userPassword, 9);
// result = await this.user.update({userPassword : pass}, {
// where : {username : user.username}
// });

result = await this.user.findByPk(user.id);
if(result) {
result.userPassword = pass;
result.save();

return result;
}
} catch (e) {
logEvent.emit('APP_ERROR', {
logTitle : '[UPDATE-USER-FAILED]',
logMessage : e
});

throw new Error(e);
}
}

最佳答案

这是一个工作示例:

user.services.ts:

import bcrypt from 'bcrypt';

const logEvent = { emit(event, payload) {} };

class UserService {
user;
constructor(user) {
this.user = user;
}
async updateUser(user) {
let result;
try {
const pass = bcrypt.hashSync(user.userPassword, 9);
result = await this.user.findByPk(user.id);
if (result) {
result.userPassword = pass;
await result.save();

return result;
}
} catch (e) {
logEvent.emit('APP_ERROR', {
logTitle: '[UPDATE-USER-FAILED]',
logMessage: e,
});

throw new Error(e);
}
}
}
export { UserService };

user.services.test.ts:

import { UserService } from './user.services';

describe('60468548', () => {
it('should update user that input by', async () => {
const dataInput = { id: 1, username: 'teguh', userPassword: 'apake' };
const userModelInstanceMock = { save: jest.fn() };
const userModelMock = { findByPk: jest.fn().mockResolvedValueOnce(userModelInstanceMock) };
const userService = new UserService(userModelMock);
const actual = await userService.updateUser(dataInput);
expect(userModelMock.findByPk).toBeCalledWith(1);
expect(userModelInstanceMock.save).toBeCalledTimes(1);
});
});

带有覆盖率报告的单元测试结果:

 PASS  src/examples/stackoverflow/60468548/user.services.test.ts
60468548
✓ should update user that input by (36ms)

------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
------------------|----------|----------|----------|----------|-------------------|
All files | 84.62 | 50 | 66.67 | 84.62 | |
user.services.ts | 84.62 | 50 | 66.67 | 84.62 | 22,27 |
------------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.78s

关于node.js - 如何用 Jest 模拟函数 .save() sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60468548/

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