Q L*_*Liu 6 javascript oop testing unit-testing node.js
在 Node.js 中测试面向对象的 javascript 是否有最佳实践?
例如,如果我有以下 Cat.js 类:
function Cat(age, name) {
this.name = name || null;
this.age = age || null;
}
Cat.prototype.getAge = function() {
return this.age;
}
Cat.prototype.setAge = function(age) {
this.age = age;
}
Cat.prototype.getName = function(name) {
return this.name;
}
Cat.prototype.setName = function(name) {
this.name = name;
}
Cat.prototype.equals = function(otherCat) {
return otherCat.getName() === this.getName()
&& otherCat.getAge() === this.getAge();
}
Cat.prototype.fill = function(newFields) {
for (var field in newFields) {
if (this.hasOwnProperty(field) && newFields.hasOwnProperty(field)) {
if (this[field] !== 'undefined') {
this[field] = newFields[field];
}
}
}
};
module.exports = Cat;
Run Code Online (Sandbox Code Playgroud)
我想编写类似于 Java 的 junit 测试的测试类:
var cat1;
setUp() {
cat1 = new Cat(10, 'Tom');
}
Run Code Online (Sandbox Code Playgroud)