有没有关于在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;
我希望写出像Java的junit测试一样的测试类:
var cat1;
setUp() {
cat1 = new Cat(10, 'Tom');
}