【问题标题】:Test cases getting failed when trying to do with mocha and chai尝试使用 mocha 和 chai 时测试用例失败
【发布时间】: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


    【解决方案1】:

    您显示的错误是由于无限递归。您应该将保存 setter 和 getter 实际值的变量命名为与 setter 和 getter 本身不同。这是width 的示例:

    class Rectangle {
        private _width: number;
    
        constructor(width: number) {
            this._width = width;
        }
    
        get width(): number {
            return this._width;
        }
    
        set width(value: number) {
            this._width = value;
        }
    }
    
    const rectangle = new Rectangle(10);
    
    console.log(rectangle.width);
    rectangle.width = 100;
    console.log(rectangle.width);
    // Cannot do this, as it won't compile: rectangle.width = "platypus";
    

    否则,使用您的代码,当构造函数执行this.width = ... 时,会调用setter,而this.width = ... 会再次调用setter,一次又一次,...

    Nitzan 是正确的,您必须调用 chai.should(),而不仅仅是访问它。

    【讨论】:

      【解决方案2】:

      如果您查看docs for Should,您会看到:

      var should = require('chai').should() //实际调用函数

      所以您的代码需要如下所示:

      import * as chai from 'chai';
      ...
      const should = chai.should();
      

      另外,在你的情况下不需要使用path,你应该可以这样做:

      let Rectangle = require('../rectangle.js');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-03-07
        • 2013-04-11
        • 1970-01-01
        • 2018-02-27
        • 1970-01-01
        • 1970-01-01
        • 2017-02-18
        相关资源
        最近更新 更多