使用Sinon从同一文件导出的两种方法

发布于 2025-02-07 22:01:24 字数 1069 浏览 4 评论 0原文

我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2025-02-14 22:01:24

找到解决方案并在此处共享,以防其他人需要它:D

修改sol.js如下所示,

const A = require('/F1');


somefunction() {



   A.B( param1, param2, (err, result) => {
      A( {anotherParam, result}, 
         function (err, result2) {
             // do something with result2

         }
      ) 
   })

}

更改了单元测试中的固执:

B_stub = sinon
      .stub()
      .callsFake((param1, param2, callback) => callback(null, result1));

A_stub = sinon
      .stub()
      .callsFake(({ param1, param2 }, cb) => cb(null, result2));

A_stub.B = B_stub;

return proxyquire(
     '/F1': A_stub,
)

Found the solution and sharing it here in case someone else needs it :D

Modified Sol.js as below

const A = require('/F1');


somefunction() {



   A.B( param1, param2, (err, result) => {
      A( {anotherParam, result}, 
         function (err, result2) {
             // do something with result2

         }
      ) 
   })

}

Changed the stubbing in unit test :

B_stub = sinon
      .stub()
      .callsFake((param1, param2, callback) => callback(null, result1));

A_stub = sinon
      .stub()
      .callsFake(({ param1, param2 }, cb) => cb(null, result2));

A_stub.B = B_stub;

return proxyquire(
     '/F1': A_stub,
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文