如何模拟 class 在函数内部实例化 - Sinon?

How to mock class instantiated inside function - Sinon?

假设我有一个像follow这样的函数。

import NetworkService from './services';

async function sendAPIRequest(data: any){
  // validations
  const service = new NetworkService();
  await service.call(data)
}

我的测试看起来像这样,它使用了 mocha、chai、sinon。

describe('sendAPIRequest', function(){
   it('make api', async function(){

       // trying to mock Network service like below
       const serviceMock = sinon.createStubInstance(NetworkService);
       await sendAPIRequest({name: 'foobar'})
   });
});

但我收到类似

的错误

Error: Expected to stub methods on object but found none

如何在测试 sendAPIRequest 时模拟我的 NetworkService.

sinon.createStubInstance() API 不会将导入的 NetworkService 的原始 call 方法替换为存根方法。它只是创建一个存根实例,因此您需要将这个存根实例传递到您的 sendAPIRequest 中并使用它。这意味着您应该将其用作依赖注入模式。

有两种方法可以测试您的代码:

  1. NetworkService.prototype
  2. 中存入 call 方法

index.ts:

import NetworkService from "./services";

export async function sendAPIRequest(data: any) {
  const service = new NetworkService();
  await service.call(data);
}

services.ts:

export default class NetworkService {
  public async call(data) {
    return "real implementation";
  }
}

index.test.ts:

import sinon from "sinon";
import NetworkService from "./services";
import { sendAPIRequest } from "./";

describe("sendAPIRequest", function() {
  afterEach(() => {
    sinon.restore();
  });

  it("should make api", async () => {
    const callStub = sinon.stub(NetworkService.prototype, "call");
    await sendAPIRequest({ name: "foobar" });
    sinon.assert.calledWithExactly(callStub, { name: "foobar" });
  });
});

包含覆盖率报告的单元测试结果:

 sendAPIRequest
    ✓ should make api


  1 passing (12ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |       95 |      100 |    83.33 |    94.44 |                   |
 index.test.ts |      100 |      100 |      100 |      100 |                   |
 index.ts      |      100 |      100 |      100 |      100 |                   |
 services.ts   |       75 |      100 |       50 |       75 |                 3 |
---------------|----------|----------|----------|----------|-------------------|
  1. 使用proxyquire模块

index.test.ts

import proxyquire from "proxyquire";
import sinon from "sinon";

describe("sendAPIRequest", function() {
  afterEach(() => {
    sinon.restore();
  });

  it("make api", async function() {
    const networkServiceInstanceStub = {
      call: sinon.stub(),
    };
    const NetworkServiceStub = sinon.stub().callsFake(() => networkServiceInstanceStub);
    const { sendAPIRequest } = proxyquire("./", {
      "./services": {
        default: NetworkServiceStub,
      },
    });
    await sendAPIRequest({ name: "foobar" });
    sinon.assert.calledOnce(NetworkServiceStub);
    sinon.assert.calledWithExactly(networkServiceInstanceStub.call, { name: "foobar" });
  });
});

包含覆盖率报告的单元测试结果:

  sendAPIRequest
    ✓ make api (279ms)


  1 passing (286ms)

---------------|----------|----------|----------|----------|-------------------|
File           |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files      |    95.24 |      100 |    85.71 |       95 |                   |
 index.test.ts |      100 |      100 |      100 |      100 |                   |
 index.ts      |      100 |      100 |      100 |      100 |                   |
 services.ts   |       75 |      100 |       50 |       75 |                 3 |
---------------|----------|----------|----------|----------|-------------------|

相关post:

  • Why is the constructor invoked despite calling createStubInstance?

  • Can ES6 constructors be stubbed more easily with Sinon?

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/Whosebug/59897060