0

我正在使用 JsTestDriver 来测试和开发 Javascript 中的某些东西。

我定期遇到这种 AssertError:

 expected [object] but was {...}

其他时候,可能是:

 expected [object] but was [object]

最后,它也可以是:

 expected {x=1,...} but was {x=2,...}

因此,我得出结论,[object] 将是我使用“new”关键字创建的对象的“实例”。表示对象的每个属性的集合可以是原始对象的复制版本。

如果是这样,这是否意味着当我处于这样的情况时:

function() {
    ...
    var obj1 = fctThatReturnsAnObject();
    obj2.addChild(obj1);
    ...
};

由于 obj1 是函数范围内的变量,它会制作原始对象的副本集合吗?

如果是这种情况,有没有办法通过引用获取对象而不是制作它的集合副本?

谢谢!

PS:这个问题的扩展是:如何更改“[对象]”以获得更多有用的信息?

4

1 回答 1

0

The solution I've come up with is this:

At first, I've changed the obj.prototype.toString = function() {} to return a string that showed the important informations. But when you run a test, the assertSame("...", obj1, obj2) seems to not use the toString function when 2 [object] are not the Same.

So, the next best thing I can think of: is to add --captureConsole when we run the tests and console.log(obj1) and console.log(obj2) to understand which object is what.

 java -jar JsTestDriver --captureConsole --tests all 
于 2012-02-12T09:28:48.097 回答