gpt4 book ai didi

javascript - 是否有像 Junit for Javascript 这样的框架来测试 Node.js 中的面向对象的 javascript?

转载 作者:搜寻专家 更新时间:2023-11-01 00:02:17 24 4
gpt4 key购买 nike

<分区>

在 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');
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com