我是Node.js的新手,我对测试有疑问,我无法弄清楚问题,这是我的错误:
gradeTests√可以增加新的等级 ×平均成绩
AssertionError:NaN == 75 at Object.equal(C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ lib \ types.js:83:39) 在Object.exports.Can avarage成绩(C:\ Users \ efi \ Desktop \ nodeFolder \ tests \ gradeTests.js:21:7) 在C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ lib \ core.js:232:20 在C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:168:13 在C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:131:25 在C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:165:17 在C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:463:34 at Object.exports.setUp(C:\ Users \ efi \ Desktop \ nodeFolder \ tests \ gradeTests.js:5:2) 在C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ lib \ core.js:260:35 在C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:458:21 在C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:163:13 在iterate(C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:123:13) at async.forEachSeries(C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:139:9) at _asyncMap(C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:162:9) at Object.mapSeries(C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:152:23) at Object.async.series(C:\ Users \ efi \ AppData \ Roaming \ npm \ node_modules \ nodeunit \ deps \ async.js:456:19)
失败:1/2断言失败(16ms)
这是我的文件:
var gradeBook = {
_grades: [],
addGrade: function(newGrade) {
this._grades.push(newGrade);
},
getCountOfGrades: function() {
return this._grades.lenght;
},
getAverage: function() {
var total = 0;
for(var i = 0; i < this._grades.lenght; i +=1 ) {
total += this._grades[i];
};
return total / this._grades.lenght;
},
reset: function() {
this._grades = [];
}
};
exports.book = gradeBook;
和
var book = require("../lib/grades").book;
exports["setUp"] = function(callback) {
book.reset();
callback();
};
exports["Can add new grade"] = function(test) {
book.addGrade(90);
var count = book.getCountOfGrades();
test.equal(count);
test.done();
};
exports["Can avarage grades"] = function(test) {
book.addGrade(100);
book.addGrade(50);
var average = book.getAverage();
test.equal(average, 75);
test.done();
};
答案 0 :(得分:1)
您的代码中存在拼写错误,lenght
应为length
。