【问题标题】:Writing tests for async.parallel.为 async.parallel 编写测试。
【发布时间】:2017-07-24 23:06:31
【问题描述】:

我是 javascript 的新手。我正在尝试使用 proxyquire 编写基本的单元测试。下面的代码。

文件 A:

const modA=require('./modA');
const modB=require('./modB');
const async=require('async');
module.exports=function(a,b,callback){

async.parallel([
   function(callback){
      // db call
       modA(a,b,callback);
   },
   function(callback){
       // db call
       mobB(a,b,callback);
   }
],
//not able to test code
(err,res){
     //do something    
});

};

文件 A 的单元测试如下所示:

const proxyquire=require('proxyquire');


function modaAStub(a, b, callback) {
  return (null, modAresponse);
}

function modaBStub(a, b, callback) {
  return (null, modaBresponse);
}

describe('test suite', () => {
  it('test: should return results', (done) => {
    const fileA = proxyquire('../../fileA', {
      './modA': modaAStub,
      './modB': modaBStub
    });

    fileA(someinput1,someinput2);
  done();
  });
});

问题是,我无法弄清楚如何测试 fileA 中具有“//做某事”的代码。

欣赏任何指针/代码。

谢谢。

【问题讨论】:

    标签: javascript node.js jasmine async.js proxyquire


    【解决方案1】:

    您可以传入另一个函数,并将其用作回调。

    function callbackStub(err, result) {
        return (null, modAresponse);
    }
    

    然后在你的文件 A 中:

    // import the callback stub above
    async.parallel([
    function(callback){
         // db call
           modA(a,b,callback);
       },
       function(callback){
           // db call
           mobB(a,b,callback);
       }
    ],callbackStub);
    

    【讨论】:

    • 我很困惑。我正在尝试为 fileA 编写测试。如果我更改我的文件 A,我正在更改我的实际代码。我可能会遗漏一些东西。
    • 我真的不明白你要测试什么——理论上,测试应该验证 .parallel 函数将正确的输出返回给回调。如果你想单独测试回调,甚至不需要让它成为并行逻辑的一部分,你可以将输入模拟为它的输入。
    • 您可以将输入模拟为它的输入-您能否详细说明一下。我遇到的问题是 modA,modB 是 db 调用。所以我只是希望它们被剔除至于输出是什么,只验证模拟结果。这只是为了测试 //do something 中的逻辑。我不希望异步本身的实际运行。
    • 谢谢乔希。感谢您的指点。我太愚蠢了,无法理解您在说什么。
    【解决方案2】:

    好吧,我太笨了。fileA 的单元测试如下所示。

        const proxyquire=require('proxyquire');
        const async=require('async');
        const expect=require('chai').expect;
    
        const modAresponse={
          a:'1'
         };
         const modBresponse={
          b:'1'
         };
        const a={
          a:'1'
         };
         const b={
          b:'1'
         };
         const someresponse={
          a:'1',
          b:'1'
         };
        function modaAStub(a, b, callback) {
          return callback(null, modAresponse);
        }
    
        function modaBStub(a, b, callback) {
          return callback(null, modaBresponse);
        }
    
        describe('test suite', () => {
          it('test: should return results', (done) => {
            const fileA = proxyquire('../../fileA', {
              './modA': modaAStub,
              './modB': modaBStub,
               async
            });
    
            fileA(a,b,(err,response)=>{
                      expect(JSON.stringify(response)).to.equal(JSON.stringify(someresponse));
            });
          done();
          });
        });
    

    【讨论】:

    • 看起来很棒!很高兴你明白了
    猜你喜欢
    • 2011-02-23
    • 1970-01-01
    • 2020-10-20
    • 2021-09-29
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多