为什么在Sinon.js上无法使用服务方法?

时间:2019-03-27 15:48:12

标签: typescript unit-testing sinon sinon-chai

由于某种原因,我无法使用服务方法。

我的服务包含一个名为“ complete ”的方法,我想将其存根以验证输入参数。 (这可能不是最好的,但我知道它应该可以工作)。无论如何,该方法不存根。

describe('test suite', () => {
  const httpMocks = require('node-mocks-http');
  const service = new MyService(sinon.createStubInstance(MyRepository));
  const controller = new MyController(service);
  const request = httpMocks.createRequest();
  const response = httpMocks.createResponse();
  let methodStub: sinon.SinonStub;

  beforeEach(async () => {
    methodStub = sinon.stub(service, 'complete');
    methodStub.callsFake(async (
      arg1: string,
      arg2: string
    ) => {
      chai.assert(arg2 === 'something');
      Promise.resolve();
    });
  });

  afterEach(async () => {
    methodStub.restore();
  });

  it.only('should complete a task', async () => {
    await controller.complete(request, response);

    chai.assert(response.statusCode === 200, 'response should have statusCode 200 as successful');
    chai.assert(methodStub.called, 'methodStub has been called');
  });
});

我应该怎么做才能正确存根?我在做什么错了?

感谢您的帮助!

0 个答案:

没有答案