sinon.js : 存根调用不会增加 callCount
sinon.js : stub call does not increment callCount
sinon.js 9.0.2
在测试中我创建了一个存根:
test.js
const myHandler = {
handle: (some_var_to_pass) => sinon.stub()["resolves"]([{ whatever: "whatever" }]),
};
然后将测试中的实际代码作为一些端点请求(使用 async / await
)执行以下代码:
code.js
module.exports = (myHandler) => ({
getStuff: async (req, res, next) => {
const var1 = "something1";
const var2 = "something2";
const my_response = await myHandler.handle()(var1, var2);
console.log("my response", JSON.stringify(my_response)); //correct, returns { whatever: "whatever"}
console.log("Executed stub: ", myHandler.handle(some_var_to_pass).callCount); //always returns zero
我控制 myHandler.handle
正确地完成存根创建并且 my_response
是
{ whatever: "whatever"}
但是,myHandler.handle().callCount
仍为零(而不是 undefined
)。我缺少什么才能使其正确增加?或者我需要在创建存根时实际将 .callCount
return 设置为 1(会很奇怪)?
您没有存根 myHandler.handle
方法。您只存根 myHandler.handle
方法返回的函数。单元测试方案:
code.js
:
module.exports = (myHandler) => ({
getStuff: async (req, res, next) => {
const var1 = 'something1';
const var2 = 'something2';
const my_response = await myHandler.handle()(var1, var2);
return my_response;
},
});
code.test.js
:
const code = require('./code');
const sinon = require('sinon');
describe('64468517', () => {
it('should pass', async () => {
const returnedFn = sinon.stub().resolves([{ whatever: 'whatever' }]);
const myHandler = {
handle: sinon.stub().returns(returnedFn),
};
await code(myHandler).getStuff();
expect(myHandler.handle.callCount).to.be.eq(1);
sinon.assert.calledOnce(myHandler.handle);
sinon.assert.calledWithExactly(returnedFn, 'something1', 'something2');
});
});
单元测试结果:
64468517
✓ should pass
1 passing (13ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
code.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
因为当你有行:myHandler.handle(some_var_to_pass)
,你启动了新的存根,它当然有 0 个 callCount。
如何解决:将myHandler.handle(some_var_to_pass)
保存到一个变量,调用并期望使用该变量。
示例:
const { expect } = require('chai');
const sinon = require('sinon');
const myHandler = {
// Call myHandler.handle() will return sinon.stub which resolves to array object.
handle: () => sinon.stub()['resolves']([{ whatever: 'whatever' }]),
};
describe('myHandler', function() {
it('handle', async function() {
const var1 = 'something1';
const var2 = 'something2';
// Get the stub variable.
const stub = myHandler.handle();
// const result = await myHandler.handle()(var1, var2);
// You use stub variable.
const result = await stub(var1, var2);
// Make sure the result is correct.
expect(result).to.deep.equal([{ whatever: 'whatever' }]);
// Obviously, stub callCount should be 1.
expect(stub.callCount).to.equal(1);
});
});
当我 运行 它在终端使用 mocha 时:
$ npx mocha index.test.js
myHandler
✓ handle
1 passing (3ms)
sinon.js 9.0.2
在测试中我创建了一个存根:
test.js
const myHandler = {
handle: (some_var_to_pass) => sinon.stub()["resolves"]([{ whatever: "whatever" }]),
};
然后将测试中的实际代码作为一些端点请求(使用 async / await
)执行以下代码:
code.js
module.exports = (myHandler) => ({
getStuff: async (req, res, next) => {
const var1 = "something1";
const var2 = "something2";
const my_response = await myHandler.handle()(var1, var2);
console.log("my response", JSON.stringify(my_response)); //correct, returns { whatever: "whatever"}
console.log("Executed stub: ", myHandler.handle(some_var_to_pass).callCount); //always returns zero
我控制 myHandler.handle
正确地完成存根创建并且 my_response
是
{ whatever: "whatever"}
但是,myHandler.handle().callCount
仍为零(而不是 undefined
)。我缺少什么才能使其正确增加?或者我需要在创建存根时实际将 .callCount
return 设置为 1(会很奇怪)?
您没有存根 myHandler.handle
方法。您只存根 myHandler.handle
方法返回的函数。单元测试方案:
code.js
:
module.exports = (myHandler) => ({
getStuff: async (req, res, next) => {
const var1 = 'something1';
const var2 = 'something2';
const my_response = await myHandler.handle()(var1, var2);
return my_response;
},
});
code.test.js
:
const code = require('./code');
const sinon = require('sinon');
describe('64468517', () => {
it('should pass', async () => {
const returnedFn = sinon.stub().resolves([{ whatever: 'whatever' }]);
const myHandler = {
handle: sinon.stub().returns(returnedFn),
};
await code(myHandler).getStuff();
expect(myHandler.handle.callCount).to.be.eq(1);
sinon.assert.calledOnce(myHandler.handle);
sinon.assert.calledWithExactly(returnedFn, 'something1', 'something2');
});
});
单元测试结果:
64468517
✓ should pass
1 passing (13ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
code.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
因为当你有行:myHandler.handle(some_var_to_pass)
,你启动了新的存根,它当然有 0 个 callCount。
如何解决:将myHandler.handle(some_var_to_pass)
保存到一个变量,调用并期望使用该变量。
示例:
const { expect } = require('chai');
const sinon = require('sinon');
const myHandler = {
// Call myHandler.handle() will return sinon.stub which resolves to array object.
handle: () => sinon.stub()['resolves']([{ whatever: 'whatever' }]),
};
describe('myHandler', function() {
it('handle', async function() {
const var1 = 'something1';
const var2 = 'something2';
// Get the stub variable.
const stub = myHandler.handle();
// const result = await myHandler.handle()(var1, var2);
// You use stub variable.
const result = await stub(var1, var2);
// Make sure the result is correct.
expect(result).to.deep.equal([{ whatever: 'whatever' }]);
// Obviously, stub callCount should be 1.
expect(stub.callCount).to.equal(1);
});
});
当我 运行 它在终端使用 mocha 时:
$ npx mocha index.test.js
myHandler
✓ handle
1 passing (3ms)