假设我具有以下功能。
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
。
sinon.createStubInstance()
API不会用存根(stubbed)替换导入的call
的原始NetworkService
方法。它只是创建一个存根实例,因此您需要将此存根实例传递到您的sendAPIRequest
中并使用它。这意味着您应该将其用作依赖项注入模式。
有两种方法来测试您的代码:
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 |
---------------|----------|----------|----------|----------|-------------------|
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