【发布时间】:2019-05-22 04:34:37
【问题描述】:
在测试套件上,我想使用链接方法 findOne 然后 select 模拟模型
登录服务
public loggingIn = async (loginDTO: LoginDTO) => {
const user = await UserModel.findOne({ email : loginDTO.email }).select(['_id', 'username', 'email', 'password']);
if (user) {
const isPasswordMatching = await bcrypt.compare(loginDTO.password, user.password);
if (isPasswordMatching) {
const token = this.generateToken(user);
const tokenDTO : TokenDTO = {
access_token: token,
expiresIn: loginConstant.EXPIRES_IN,
};
return tokenDTO;
}
throw new InvalidCrendentialsException();
}
throw new InvalidCrendentialsException();
}
测试
it('should return access_token when login is success', async () => {
UserModel.findOne = jest.fn().mockResolvedValueOnce(UserFactory.successResponse);
bcrypt.compare = jest.fn().mockResolvedValueOnce(true);
const loginController = new LoginController();
const app = new App([loginController]);
const result = await request(app.getServer())
.post(`${loginController.path}`)
.send(loginFactory.validRequest);
// expect(result.status).toBe(200);
expect(result.body).toBe(200);
});
错误消息
user_model_1.default.findOne(...).select 不是函数
【问题讨论】:
标签: node.js unit-testing mocking jestjs method-chaining