如何模拟 NodeJS 中的嵌套依赖项
How to mock nested dependencies in NodeJS
我有一个模块a
const b = require(./b);
function aGetResult() {
return b.getInfo();
}
模块 B
const c = require(./c);
function getInfo() {
return getDetailInfo();
}
function getDetailInfo() {
const result = c.getApiResult();
return result
}
模块 C
function getApiResult() {
return api.get(/test/1);
}
我已经为模块 A 编写了一个测试,但是 运行 遇到了存根问题 dependencies.I 只想存根 c.getApiResult()
而不是 b.getInfo()
或 b.getDetailInfo()
。我试过使用 proxyquire
有选择地存根,但遇到了问题。有帮助吗?
您应该使用 proxyquire
包的 Globally override require。
a
取决于 b
,b
取决于 c
。现在你想在测试 a
时模拟间接 c
依赖而不是直接 b
依赖。 不建议这样做。但无论如何,这是解决方案:
例如
a.js
:
const b = require('./b');
function aGetResult() {
return b.getInfo();
}
exports.aGetResult = aGetResult;
b.js
:
const c = require('./c');
function getInfo() {
return getDetailInfo();
}
function getDetailInfo() {
const result = c.getApiResult();
return result;
}
module.exports = { getInfo };
c.js
:
const api = {
get(url) {
return 'real result';
},
};
function getApiResult() {
return api.get('/test/1');
}
module.exports = { getApiResult };
a.test.js
:
const proxyquire = require('proxyquire');
const { expect } = require('chai');
const sinon = require('sinon');
describe('63275147', () => {
it('should pass', () => {
const stubs = {
'./c': {
getApiResult: sinon.stub().returns('stubbed result'),
'@global': true,
},
};
const a = proxyquire('./a', stubs);
const actual = a.aGetResult();
expect(actual).to.be.eq('stubbed result');
sinon.assert.calledOnce(stubs['./c'].getApiResult);
});
});
单元测试结果:
63275147
✓ should pass (2630ms)
1 passing (3s)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 83.33 | 100 | 60 | 83.33 |
a.js | 100 | 100 | 100 | 100 |
b.js | 100 | 100 | 100 | 100 |
c.js | 50 | 100 | 0 | 50 | 3-8
----------|---------|----------|---------|---------|-------------------
我有一个模块a
const b = require(./b);
function aGetResult() {
return b.getInfo();
}
模块 B
const c = require(./c);
function getInfo() {
return getDetailInfo();
}
function getDetailInfo() {
const result = c.getApiResult();
return result
}
模块 C
function getApiResult() {
return api.get(/test/1);
}
我已经为模块 A 编写了一个测试,但是 运行 遇到了存根问题 dependencies.I 只想存根 c.getApiResult()
而不是 b.getInfo()
或 b.getDetailInfo()
。我试过使用 proxyquire
有选择地存根,但遇到了问题。有帮助吗?
您应该使用 proxyquire
包的 Globally override require。
a
取决于 b
,b
取决于 c
。现在你想在测试 a
时模拟间接 c
依赖而不是直接 b
依赖。 不建议这样做。但无论如何,这是解决方案:
例如
a.js
:
const b = require('./b');
function aGetResult() {
return b.getInfo();
}
exports.aGetResult = aGetResult;
b.js
:
const c = require('./c');
function getInfo() {
return getDetailInfo();
}
function getDetailInfo() {
const result = c.getApiResult();
return result;
}
module.exports = { getInfo };
c.js
:
const api = {
get(url) {
return 'real result';
},
};
function getApiResult() {
return api.get('/test/1');
}
module.exports = { getApiResult };
a.test.js
:
const proxyquire = require('proxyquire');
const { expect } = require('chai');
const sinon = require('sinon');
describe('63275147', () => {
it('should pass', () => {
const stubs = {
'./c': {
getApiResult: sinon.stub().returns('stubbed result'),
'@global': true,
},
};
const a = proxyquire('./a', stubs);
const actual = a.aGetResult();
expect(actual).to.be.eq('stubbed result');
sinon.assert.calledOnce(stubs['./c'].getApiResult);
});
});
单元测试结果:
63275147
✓ should pass (2630ms)
1 passing (3s)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 83.33 | 100 | 60 | 83.33 |
a.js | 100 | 100 | 100 | 100 |
b.js | 100 | 100 | 100 | 100 |
c.js | 50 | 100 | 0 | 50 | 3-8
----------|---------|----------|---------|---------|-------------------