2014-01-28 37 views
0

嗨,我只想問是否有一種方法可以調用module.exports內的循環?dalekjs,在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循環的任何想法?

回答

2

從我看到的情況來看,這只是一個簡單的語法錯誤。 您在前面缺少test

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

這必須變成:

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

+0

此制定出適合我。謝謝,dalekjs還能夠調用module.exports的外部函數嗎? –

+0

當然。你可以要求你想要的一切並執行它。一切都只是「正常」node.js/javascript代碼:) –