AssertionError:应使用参数201调用存根 [英] AssertionError: expected stub to have been called with arguments 201

查看:11
本文介绍了AssertionError:应使用参数201调用存根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对mocha sinon单元测试不熟悉。这就是我想要得到的预期结果。但它抛出了以下错误。

AssertionError:应使用参数201调用存根

如果我做错了什么,有人能帮我解决这个问题或提出任何建议吗?

async createUser(req: Request, res: Response, next: NextFunction) {
    const { name, email, password } = req.body;
    const user = new User({ name, email, password });

    try {
      const userCreated = await userService.createUserService(user);
      res.status(201).json({ status: 201, data: userCreated });
    } catch (err) {
      next(httpErrors(500, err.message));
    }
  }

单元测试以上:

import sinon from "sinon";
import { Request, Response } from "express";

import userController from "./user.controller";
import UserService from "../services/user.service";
import User from "../models/user.model";

describe("UserController", () => {
  let userService;

  beforeEach(() => {
    userService = new UserService();
  });

  it("should create user", async () => {
    const mReq = {
      body: {
        name: "mockName",
        email: "mockEmail",
        password: "mockPassword#123"
      }
    } as Request;

    const mRes = {
      status: sinon.stub().returnsThis(),
      json: sinon.stub()
    } as any;

    const mNext = sinon.stub();
    const stubValue = {
      name: "Deepesh"
    };
    const createUserServiceStub = sinon
      .stub(userService, "createUserService")
      .resolves(stubValue);

    await userController.createUser(mReq, mRes, mNext);
    sinon.assert.calledWithExactly(
      createUserServiceStub,
      new User({
        name: "mockName",
        email: "mockEmail",
        password: "mockPassword#123"
      })
    );
    sinon.assert.calledWithExactly(mRes.status, 201);
    sinon.assert.calledWithExactly(mRes.json, {
      status: 201,
      data: { name: "StubName" }
    });
  });
});

user.service.ts

import User, { IUser } from "../models/user.model";
import HashPassword from "../utils/password";

class UserService {
  private user;

  constructor() {
    this.user = User;
  }

  /**
   * Function to check existence of user if not then create new user in db
   * @param user
   */
  public async createUserService(user: IUser): Promise<any> {
    try {
      // Check for existence of user in db
      const userRecord = await this.user.findOne({ email: user.email });
      if (userRecord) {
        throw new Error("RECORD ALREADY EXISTS");
      }

      const encPwd = await HashPassword.encryptPassword(user.password); // Encrypt user entered password
      user.password = encPwd;
      user.role = "user";

      const createdUser = await this.user.create(user); // create user
      return createdUser;
    } catch (error) {
      throw error;
    }
  }
}

export default UserService;

如有任何帮助,我们将不胜感激。

提前谢谢。

推荐答案

以下是单元测试解决方案:

controller.ts

import { NextFunction, Response, Request } from 'express';
import userService from './service';
import User from './model';

const userController = {
  async createUser(req: Request, res: Response, next: NextFunction) {
    const { name, email, password } = req.body;
    const user = new User({ name, email, password });

    try {
      const userCreated = await userService.createUserService(user);
      res.status(201).json({ status: 201, data: userCreated });
    } catch (err) {
      next(err);
    }
  },
};

export default userController;

service.ts

import User from './model';

const userService = {
  async createUserService(user: User) {
    return {};
  },
};

export default userService;

model.ts

export default class User {
  constructor(data) {}
}

controller.test.ts

import userController from './controller';
import { Request } from 'express';
import userService from './service';
import sinon from 'sinon';
import User from './model';

describe('userController', () => {
  it('should create user', async () => {
    const mReq = { body: { name: 'mockName', email: 'mockEmail', password: 'mockPassword#123' } } as Request;
    const mRes = { status: sinon.stub().returnsThis(), json: sinon.stub() } as any;
    const mNext = sinon.stub();
    const stubValue = {
      name: 'StubName',
    };
    const createUserServiceStub = sinon.stub(userService, 'createUserService').resolves(stubValue);
    await userController.createUser(mReq, mRes, mNext);
    sinon.assert.calledWithExactly(
      createUserServiceStub,
      new User({
        name: 'mockName',
        email: 'mockEmail',
        password: 'mockPassword#123',
      }),
    );
    sinon.assert.calledWithExactly(mRes.status, 201);
    sinon.assert.calledWithExactly(mRes.json, { status: 201, data: { name: 'StubName' } });
  });
});

单元测试结果和覆盖率报告:

  userController
    ✓ should create user


  1 passing (25ms)

---------------|---------|----------|---------|---------|-------------------
File           | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
---------------|---------|----------|---------|---------|-------------------
All files      |   85.71 |      100 |   66.67 |   85.71 |                   
 controller.ts |      90 |      100 |     100 |      90 | 14                
 model.ts      |     100 |      100 |     100 |     100 |                   
 service.ts    |   66.67 |      100 |       0 |   66.67 | 5                 
---------------|---------|----------|---------|---------|-------------------

这篇关于AssertionError:应使用参数201调用存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆