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

问题描述 投票:1回答:1

假设我具有以下功能。

import NetworkService from './services';

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

我的测试看起来像这样,使用了摩卡,柴,西农。

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

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

但是我遇到了类似错误

错误:应在对象上存入方法,但未找到任何方法

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

typescript unit-testing express mocking sinon
1个回答
1
投票

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

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

  1. call中存根NetworkService.prototype方法

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 |
---------------|----------|----------|----------|----------|-------------------|

相关帖子:

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

© www.soinside.com 2019 - 2024. All rights reserved.