gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:48:28 25 4
gpt4 key购买 nike

假设我有一个类似 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 方法替换为 stub 方法。它只是创建一个 stub 实例,因此您需要将这个 stub 实例传递到您的 sendAPIRequest 中并使用它。这意味着您应该将其用作依赖项注入(inject)模式。

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

  1. NetworkService.prototype 中存入 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 |
---------------|----------|----------|----------|----------|-------------------|

相关文章:

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

关于typescript - 如何模拟在函数内部实例化的类 - Sinon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59897060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com