2014-01-27 91 views
0

我有一個測試應用程序,有一個用戶界面,允許用戶選擇某些測試,並通過點擊一個按鈕與摩卡運行這些測試。出於某種原因,第一次,測試運行,我得到了傳球結果(2傳球或其他)。即使選擇了所有相同的值,任何後續點擊按鈕都將運行Mocha,但它會運行0次測試。所以,它返回0傳遞。下面是當AJAX POST是由運行代碼:爲什麼第一次使用Node.js運行Mocha,但不是第二次?

var Mocha = require('mocha'), 
    fs = require('fs'), 
    path = require('path'); 

var mocha = new Mocha({ 
    reporter: 'list' 
}); 

fs.readdirSync('node_modules/selenium-webdriver/nb_tests/').filter(function (file) { 
    return file.substr(-3) === '.js'; 
}).forEach(function (file) { 
    mocha.loadFile(path.join('node_modules/selenium-webdriver/nb_tests/', file)); 
}); 

// Now, you can run the tests. 
mocha.run(function (failures) { 
    process.on('exit', function() { 
     process.exit(failures); 
    }); 
}); 

順便說一句,當我的權利在運行測試之前CONSOLE.LOG(摩卡),他們對所有請求相同。任何想法可能會導致這個問題?

回答

1

想通了......摩卡似乎不喜歡我爲每個POST創建一個新實例。上面的代碼都包含在路由處理程序中。所以,這裏是我所做的:

var Mocha = require('mocha'); 
var path = require('path'); 
var fs = require('fs'); 

var mocha = new Mocha({ 
    reporter: 'list' 
}); 

app.post('/runtest', function (req, res) { 
    fs.readdirSync('node_modules/selenium-webdriver/nb_tests/').filter(function (file) { 
     return file.substr(-3) === '.js'; 
    }).forEach(function (file) { 
     mocha.addFile(path.join('node_modules/selenium-webdriver/nb_tests/', file)); 
    }); 

    mocha.run(); 
}); 
相關問題