【问题标题】:Vows JS Testing for UndefinedVows JS 测试未定义
【发布时间】:2013-11-14 23:48:58
【问题描述】:

我正在尝试使用 vows js 来创建单元测试。当“主题”是“未定义”时,我遇到了麻烦。请看下面的例子:

var vows = require('vows'),
  assert = require('assert');

function giveMeUndefined(){
  return undefined;
}

vows.describe('Test vow').addBatch({
  'When the topic is undefined': {
    topic: function() {
      return giveMeUndefined();
    },
    'should return the default value of undefined.': function(topic) {
      assert.isUndefined(topic);
    }
  }
}).export(module);

这不是确切的代码,但它是它的要点。当我运行测试时,我得到“回调未触发”。单步浏览 vows 的代码,我可以看到它在 topic 为undefined 时分支。

最终我想知道如何编写单元测试来做到这一点。我团队中的其他人写了我认为是 hack 的内容,并在主题中做了断言,如果 topic === undefined 则返回 truefalse

【问题讨论】:

    标签: javascript node.js unit-testing vows


    【解决方案1】:

    来自 Vows 文档:

    » 主题是可以执行异步代码的值或函数。

    在您的示例中,topic 被分配给一个函数,因此 vows 期待异步代码。

    只需将您的主题改写如下:

    var vows = require('vows'),
      assert = require('assert');
    
    function giveMeUndefined(){
      return undefined;
    }
    
    vows.describe('Test vow').addBatch({
      'When the topic is undefined': {
        topic: giveMeUndefined(),
        'should return the default value of undefined.': function(topic) {
          assert.isUndefined(topic);
        }
      }
    }).export(module);
    

    【讨论】:

      【解决方案2】:

      您可以提供这样的回调:使用**Note** 观察行

      var vows = require('vows'),
        assert = require('assert');
      
      function giveMeUndefined(callback){//**Note**
        callback(undefined); //**Note**
      }
      
      vows.describe('Test vow').addBatch({
        'When the topic is undefined': {
          topic: function(){
           giveMeUndefined(this.callback); // **Note**
          },
          'should return the default value of undefined.': function(undefinedVar, ignore) {
            assert.isUndefined(undefinedVar);
          }
        }
      }).export(module);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-06
        相关资源
        最近更新 更多