【发布时间】:2023-03-08 05:22:01
【问题描述】:
我正在尝试使用 mocha 框架的简单测试用例
我写了简单的打字稿
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
get height() {
return this.height;
}
set height(value) {
if (typeof value !== 'number') {
throw new Error('"height" must be a number.');
}
this.height = value;
}
get width() {
return this.width;
}
set width(value) {
if (typeof value !== 'number') {
throw new Error('"width" must be a number.');
}
this.width = value;
}
get area() {
return this.width * this.height;
}
get circumference() {
return 2 * this.width + 2 * this.height;
}
}
var rectangle= new Rectangle(10,20);
module.exports = Rectangle;
但是当尝试编写如下测试用例时:
"use strict"
require('babel-register')({
presets: ['es2015']
});
// Import chai.
import * as chai from 'chai';
var path = require('path');
// Import the Rectangle class.
let Rectangle = require(path.join(__dirname, '..', 'rectangle.js'));
const should = chai.should;
var expect = require('chai').expect;
describe('Rectangle', () => {
describe('#width', () => {
let rectangle;
beforeEach(() => {
// Create a new Rectangle object before every test.
rectangle = new Rectangle(10, 20);
});
it('returns the width', () => {
// This will fail if "rectangle.width" does
// not equal 10.
rectangle.width.should.equal(10);
});
});
});
但是测试用例失败了
我不明白这里的错误
任何帮助将不胜感激
【问题讨论】:
标签: node.js unit-testing typescript mocha.js chai