【问题标题】:dalekjs, calling loops inside module.exports testdalekjs,在 module.exports 测试中调用循环
【发布时间】:2014-01-28 06:03:39
【问题描述】:

您好,我只是想问一下是否有一种方法可以在 module.exports 中调用循环?

module.exports = {
'GZAIS Create New Asset': function (test) {
  test

  .assert.exists('input#asset_name', 'asset input exists')
  .assert.exists('textarea#asset_description', 'asset description exists')
  .assert.exists('input#asset_type', 'asset type exists')
  .assert.exists('input#date_purchased', 'date purchased exists')
  .assert.exists('.filter-option', 'status exists')
  .assert.exists('input#serial_number', 'Serial Number exists')
  .assert.exists('input#supplier', 'Supplier field exists')
  .assert.exists('input#reason', 'Reason for purchase field exists')
  .done();
}
};

现在这是我在字段存在时断言字段的设计,我想做的是使用 for 循环来避免如此多的重复。

看起来应该是这样的

var assetInput = ['asset_name','asset_description','asset_type','date_purchased','serial_number','supplier','reason'];
module.exports = {
'GZAIS Create New Asset': function (test) {
  test
    for(var i=0; i<assetInput.length; i++){
      .assert.exists('input#'+assetInput[i], assetInput[i] + 'exists')
    }
  .done();
}
};

但问题是这段代码不起作用,你们知道如何在 module.exports 中实现循环吗?

【问题讨论】:

    标签: npm phantomjs dalekjs


    【解决方案1】:

    据我所知,这只是一个简单的语法错误。 您在前面缺少test

    .assert.exists('input#'+assetInput[i], assetInput[i] + 'exists')

    这必须变成:

    test.assert.exists('input#'+assetInput[i], assetInput[i] + 'exists')

    【讨论】:

    • 这对我来说效果很好。谢谢,dalekjs也能调用module.exports的外部函数吗?
    • 当然。你可以要求你想要的一切并执行它。一切都只是“正常”的 node.js/javascript 代码 :)
    猜你喜欢
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    相关资源
    最近更新 更多