使用Sinon从同一文件导出的两种方法
我是JavaScript的新手,需要帮助单位测试我的代码。 有一个现有文件f1.js
我在实现中使用的方法。该文件看起来像这样:
f1.js:
function A({p1, p2}, callback) {
}
function B(q1, q2) {
}
module.exports = A;
module.exports.B = callbackify(B);
我的文件sol.js
使用这些功能为,
sol.js:
const A = require('/F1');
const {
B
} = require('/F1');
somefunction() {
B( param1, param2, (err, result) => {
A( {anotherParam, result},
function (err, result2) {
// do something with result2
}
)
})
}
我正在尝试尝试单元测试方法somefunction
并与固执挣扎。这是我模拟函数的方式
sol_test.js
B_stub = sinon
.stub()
.callsFake((param1, param2, callback) => callback(null, result1));
A_stub = sinon
.stub()
.callsFake(({ param1, param2 }, cb) => cb(null, result2));
return proxyquire(
'F1' : {
A_stub,
B : B_stub,
}
)
我遇到了错误:b不是函数
I'm a newbie to JavaScript and need help in unit-testing my code.
There is an existing file F1.js
whose methods I'm using in my implementation. The file looks like this:
F1.js :
function A({p1, p2}, callback) {
}
function B(q1, q2) {
}
module.exports = A;
module.exports.B = callbackify(B);
My file Sol.js
uses these functions as,
Sol.js:
const A = require('/F1');
const {
B
} = require('/F1');
somefunction() {
B( param1, param2, (err, result) => {
A( {anotherParam, result},
function (err, result2) {
// do something with result2
}
)
})
}
I'm trying to unit test the method somefunction
and struggling with the stubbing. Here is how I mock the functions
Sol_Test.js
B_stub = sinon
.stub()
.callsFake((param1, param2, callback) => callback(null, result1));
A_stub = sinon
.stub()
.callsFake(({ param1, param2 }, cb) => cb(null, result2));
return proxyquire(
'F1' : {
A_stub,
B : B_stub,
}
)
I'm getting the error : B is not a function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到解决方案并在此处共享,以防其他人需要它:D
修改
sol.js
如下所示,更改了单元测试中的固执:
Found the solution and sharing it here in case someone else needs it :D
Modified
Sol.js
as belowChanged the stubbing in unit test :