小编dul*_*xyz的帖子

如何用玩笑模拟猫鼬链接查询

在测试套件上,我想用链接方法模拟模型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();
  }
Run Code Online (Sandbox Code Playgroud)

测试

it('should return access_token when login is success', async () => {
      UserModel.findOne = jest.fn().mockResolvedValueOnce(UserFactory.successResponse);
      bcrypt.compare = …
Run Code Online (Sandbox Code Playgroud)

unit-testing mocking method-chaining node.js jestjs

1
推荐指数
1
解决办法
2032
查看次数

标签 统计

jestjs ×1

method-chaining ×1

mocking ×1

node.js ×1

unit-testing ×1