[英]When unit testing a class method, how do I mock a function it calls--one defined in an imported module?
我正在对特定方法进行单元测试,并且遇到问题 mocking 另一个 function 在此过程中被调用。 在我的情况下,测试方法在 class 中定义,而我想模拟的 function 在单独的模块中定义。 如何模拟这个 function? 请参阅下面的代码。
过去,我使用 Sinon package 来模拟/存根依赖项(示例)。 但这在这种情况下不起作用。 这是我第一次测试 class 中定义的方法,所以也许这就是 mocking 依赖项不起作用的原因。
我的代码
包含测试 Function (myLib/myDir/combo.js) 的模块
const { externalFunction } = require('./external-function')
class Combo {
constructor(props) {}
async myMethod () {// The function under test.
externalFunction()
}
}
const myCombo = props => new Combo(props)
module.exports = { myCombo }
我的测试文件(test/myLib/myDir/combo.test.js); 没有尝试 mocking
const { myCombo } = require('../../../myLib/myDir/combo')
const comboObj = myCombo({}) // Instantiate object to expose method to test.
await comboObj.myMethod()// Call method that I want to test. This throws type error because myMethod function calls externalFunction, which throws an error in the test environment.
我的测试文件(test/myLib/myDir/combo.test.js); 尝试使用Sinon package来模拟
const sinon = require('sinon')
const dependencyModule = require('./external-function')// Defines the method dependencyModule.methodToMock
const myStub = sinon.stub(dependencyModule, 'methodToMock').returns(555) // Stubs dependencyModule.methodToMock and ensures it always returns the value: 555.
const comboObj = myCombo({}) // Instantiate object to expose method to test.
await comboObj.myMethod()// Call method that I want to test. This throws type error because myMethod function calls externalFunction, which throws an error in the test environment.
如何? 您需要遵循“存根模块不能被解构”。 在官方指南How to stub a dependency of a module
例如,我在同一目录中有文件 external-function.js、combo.js 和 test.js。 我选择使用console.log来显示存根有效并且假 function 被调用,因为你不期望在myMethod上返回一些东西。
// File: external-function.js
function externalFunction () {
console.log('Original Called');
}
module.exports = { externalFunction };
// File: combo.js
// Note: "stubbed module can not be destructured."
const externalFunction = require('./external-function')
class Combo {
constructor(props) {}
async myMethod () {
externalFunction.externalFunction()
}
}
const myCombo = props => new Combo(props)
module.exports = { myCombo };
// File: test.js
const sinon = require('sinon');
const { myCombo } = require('./combo');
const dependencyModule = require('./external-function');
describe('myCombo', () => {
it('myMethod', async () => {
sinon.stub(dependencyModule, 'externalFunction').callsFake(() => {
console.log('Fake Called');
});
const comboObj = myCombo({});
await comboObj.myMethod();
});
});
当我在终端上使用 nyc 和 mocha 运行它时:
$ npx nyc mocha test.js
myCombo
Fake Called
✓ myMethod
1 passing (3ms)
----------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------------|---------|----------|---------|---------|-------------------
All files | 85.71 | 100 | 75 | 83.33 |
combo.js | 100 | 100 | 100 | 100 |
external-function.js | 50 | 100 | 0 | 50 | 2
----------------------|---------|----------|---------|---------|-------------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.