【发布时间】:2021-01-13 23:13:04
【问题描述】:
我正在尝试为我的服务文件编写单元测试。我在测试时得到了这个
SequelizeAccessDeniedError: Access denied for user ''@'localhost' (using password: NO)
据我所知,我认为这是因为错误的数据库模拟。
ProgramService.js
class ProgramService {
constructor() {
this.subsciberProgram = new SubsciberProgram()
}
async subscribeUser(data) {
try {
const { msisdn, userId, programId, uuid } = data;
if ((await this.subsciberProgram.findBySubscriberId(userId, programId)).length) {
throw ({code:500,message:'You have already subscribed'});
}
return await this.subsciberProgram.create(userId, programId);
} catch (error) {
throw error;
}
}
}
test.spec.js
const ProgramsService = require('../src/services/program/programService')
const SubsciberProgram = require('../src/services/subscriberProgram/subsciberProgramService')
const programsService = new ProgramsService()
const subsciberProgram = new SubsciberProgram()
const db = require('./../src/models')
beforeAll(() => {
db.sequelize.sync({ force: true }).then(() => { });
});
describe('Subscribe', () => {
test('should return 200', async () => {
jest.spyOn(subsciberProgram, 'findBySubscriberId').mockResolvedValueOnce(null);
const rep = await programsService.subscribeUser(serviceRecord);
expect(rep).toBeTruthy();
});
});
------------------------------------------ -----------------------------
***** 幻灯片p2更新 *******
【问题讨论】:
-
请提供被测代码。否则,我们不知道应该模拟哪个对象
-
@slideshowp2 我已经更新了我的问题。请查找相关代码
标签: node.js unit-testing jestjs sequelize.js