如何在打字稿中测试方法?

时间:2018-08-05 15:13:52

标签: typescript unit-testing testing jestjs

告诉我,如何测试distanceCalculation()方法? 我想用精确指定的值检查测试结果的准确性。 如何在Jest中测试Typescript的方法? 这是一个测试任务,我正在学习。

class Car {
  amountOfFuel: number;
  fuelСonsumption: number;

    constructor(carAmountOfFuel: number,
      carFuelСonsumption: number) {
        this.amountOfFuel = carAmountOfFuel;
        this.fuelСonsumption = carFuelСonsumption;
      }

      distanceСalculation(): number {
        return (this.amountOfFuel / this.fuelСonsumption)*100;
    }
}

export class Bentley extends Car {
  }

export class Zhiguli extends Car {
  }

测试代码

import { Bentley, Zhiguli } from './sum'

test("property check Bentley", () => {
  let car1 = new Bentley(90, 12);

  expect(car1).toHaveProperty('amountOfFuel',90);
  expect(car1).toHaveProperty('fuelСonsumption', 12);
});

test("property check Zhiguli", () => {
  let car2 = new Zhiguli(40, 10);

  expect(car2).toHaveProperty('amountOfFuel',40);
  expect(car2).toHaveProperty('fuelСonsumption', 10);
});

1 个答案:

答案 0 :(得分:0)

如果应该从类外部访问distanceCalculation,请确保将其标记为公开。

您可以调用它,并从测试中检查返回的值,如下所示:

cv2.xfeatures2d.matchGMS