【发布时间】:2016-06-24 13:21:47
【问题描述】:
我正在 typescript 中创建一个向量库。我的第一次测试失败了:)。
这与 TypeScript/JavaScript 中的对象相等性有关,但我找不到使测试变为绿色的方法。 typescript 的官方文档http://www.typescriptlang.org/Handbook#classes 中没有提到对象相等性。
有人可以帮帮我吗?
这是源代码。
class Vector {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
add(that: Vector) {
return new Vector(this.x + that.x, this.y + that.y);
}
}
export = Vector;
然后我对这个类进行单元测试如下
var Vector = require("../lib/vector")
describe("vector", function () {
it("should add another vector", function () {
var v1 = new Vector(1, 1);
var v2 = new Vector(2, 3);
expect(v1.add(v2)).toEqual(new Vector(3, 4));
});
});
执行时得到如下错误
Failures:
1) vector should add another vector
1.1) Expected Vector({ x: 3, y: 4 }) to be Vector({ x: 3, y: 4 }).
【问题讨论】:
-
它适用于jsfiddle。
标签: typescript equality