无法使用 proxyquire 正确存根
Not able to properly stub with proxyquire
我无法理解 proxyquire。我有这个 auditEvent 方法,auditEvent.js:
的一部分
const {verify} = require('@mycompany/verifylib');
const auditEvent = () => {
blabla();
verify(); // I want to make this call do nothing
blablabla();
};
module.exports = { auditEvent };
test.js:
const sinon = require('sinon');
const proxyquire = require('proxyquire');
let verifyStub = sinon.stub();
let auditEvent = proxyquire('./auditEvent', {
'@mycompany/verifylib': {
verify: verifyStub,
'@noCallThru': true,
},
});
auditEvent(); // fails - not a valid function - what am I doing wrong?
您应该从 ./auditEvent
模块解构 auditEvent
函数。
例如
auditEvent.js
:
const { verify } = require('@mycompany/verifylib');
const auditEvent = () => {
verify();
};
module.exports = { auditEvent };
auditEvent.test.js
:
const sinon = require('sinon');
const proxyquire = require('proxyquire');
describe('68370747', () => {
it('should pass', () => {
let verifyStub = sinon.stub();
let { auditEvent } = proxyquire('./auditEvent', {
'@mycompany/verifylib': {
verify: verifyStub,
'@noCallThru': true,
},
});
auditEvent();
sinon.assert.calledOnce(verifyStub);
});
});
测试结果:
68370747
✓ should pass (1301ms)
1 passing (1s)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
auditEvent.js | 100 | 100 | 100 | 100 |
---------------|---------|----------|---------|---------|-------------------
我无法理解 proxyquire。我有这个 auditEvent 方法,auditEvent.js:
的一部分const {verify} = require('@mycompany/verifylib');
const auditEvent = () => {
blabla();
verify(); // I want to make this call do nothing
blablabla();
};
module.exports = { auditEvent };
test.js:
const sinon = require('sinon');
const proxyquire = require('proxyquire');
let verifyStub = sinon.stub();
let auditEvent = proxyquire('./auditEvent', {
'@mycompany/verifylib': {
verify: verifyStub,
'@noCallThru': true,
},
});
auditEvent(); // fails - not a valid function - what am I doing wrong?
您应该从 ./auditEvent
模块解构 auditEvent
函数。
例如
auditEvent.js
:
const { verify } = require('@mycompany/verifylib');
const auditEvent = () => {
verify();
};
module.exports = { auditEvent };
auditEvent.test.js
:
const sinon = require('sinon');
const proxyquire = require('proxyquire');
describe('68370747', () => {
it('should pass', () => {
let verifyStub = sinon.stub();
let { auditEvent } = proxyquire('./auditEvent', {
'@mycompany/verifylib': {
verify: verifyStub,
'@noCallThru': true,
},
});
auditEvent();
sinon.assert.calledOnce(verifyStub);
});
});
测试结果:
68370747
✓ should pass (1301ms)
1 passing (1s)
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
auditEvent.js | 100 | 100 | 100 | 100 |
---------------|---------|----------|---------|---------|-------------------