使用 CHAI 测试设置器和获取器
Test setters and getters with CHAI
我正在备考,其中一项任务是使用 Chai 和 Mocha 进行单元测试,但不太了解如何测试 class 是否具有 getter 和属性的设置方法。你能帮我吗?这是作业中的示例 class:
class PaymentPackage {
constructor(name, value) {
this.name = name;
this.value = value;
this.VAT = 20; // Default value
this.active = true; // Default value
}
get name() {
return this._name;
}
set name(newValue) {
if (typeof newValue !== 'string') {
throw new Error('Name must be a non-empty string');
}
if (newValue.length === 0) {
throw new Error('Name must be a non-empty string');
}
this._name = newValue;
}
get value() {
return this._value;
}
set value(newValue) {
if (typeof newValue !== 'number') {
throw new Error('Value must be a non-negative number');
}
if (newValue < 0) {
throw new Error('Value must be a non-negative number');
}
this._value = newValue;
}
get VAT() {
return this._VAT;
}
set VAT(newValue) {
if (typeof newValue !== 'number') {
throw new Error('VAT must be a non-negative number');
}
if (newValue < 0) {
throw new Error('VAT must be a non-negative number');
}
this._VAT = newValue;
}
get active() {
return this._active;
}
set active(newValue) {
if (typeof newValue !== 'boolean') {
throw new Error('Active status must be a boolean');
}
this._active = newValue;
}
}
非常简单。你得到你的 class 的实例,然后一个一个地测试 getter/setters。
describe("The class PaymentPackage", function () {
function getInstance () {
return new PaymentPackage("A", 1)
}
it("should have a getter property 'name'", function () {
const paymentPackage = getInstance()
expect(paymentPackage.name).to.be.a("string").and.equal("A")
})
it("should have a setter property 'name'", function () {
const paymentPackage = getInstance()
paymentPackage.name = "B"
expect(paymentPackage.name).to.be.a("string").and.equal("B")
})
})
我正在备考,其中一项任务是使用 Chai 和 Mocha 进行单元测试,但不太了解如何测试 class 是否具有 getter 和属性的设置方法。你能帮我吗?这是作业中的示例 class:
class PaymentPackage {
constructor(name, value) {
this.name = name;
this.value = value;
this.VAT = 20; // Default value
this.active = true; // Default value
}
get name() {
return this._name;
}
set name(newValue) {
if (typeof newValue !== 'string') {
throw new Error('Name must be a non-empty string');
}
if (newValue.length === 0) {
throw new Error('Name must be a non-empty string');
}
this._name = newValue;
}
get value() {
return this._value;
}
set value(newValue) {
if (typeof newValue !== 'number') {
throw new Error('Value must be a non-negative number');
}
if (newValue < 0) {
throw new Error('Value must be a non-negative number');
}
this._value = newValue;
}
get VAT() {
return this._VAT;
}
set VAT(newValue) {
if (typeof newValue !== 'number') {
throw new Error('VAT must be a non-negative number');
}
if (newValue < 0) {
throw new Error('VAT must be a non-negative number');
}
this._VAT = newValue;
}
get active() {
return this._active;
}
set active(newValue) {
if (typeof newValue !== 'boolean') {
throw new Error('Active status must be a boolean');
}
this._active = newValue;
}
}
非常简单。你得到你的 class 的实例,然后一个一个地测试 getter/setters。
describe("The class PaymentPackage", function () {
function getInstance () {
return new PaymentPackage("A", 1)
}
it("should have a getter property 'name'", function () {
const paymentPackage = getInstance()
expect(paymentPackage.name).to.be.a("string").and.equal("A")
})
it("should have a setter property 'name'", function () {
const paymentPackage = getInstance()
paymentPackage.name = "B"
expect(paymentPackage.name).to.be.a("string").and.equal("B")
})
})