【发布时间】:2014-10-14 07:22:42
【问题描述】:
我正在运行 Karma 规范来测试 Egghead.io 教程中概述的模型的 Angular BaseClass 的功能。
该行为似乎有效,但我遇到了一个奇怪的错误:
PhantomJS 1.9.7 (Mac OS X) BCCache adds a cache to the model FAILED
Expected { } to equal { }.
Error: Expected { } to equal { }.
What I could find of this error(很难搜索,考虑到字符——表明toEqual 应该能够识别这两个对象的等价性——所以我有点难过。
这是规范代码(coffeescript):
describe 'BCCache', ->
it "adds a cache to the model", ->
expect(Post.cached).toEqual({})
这是它正在测试的内容:
base.coffee
angular.module("BaseClass")
.factory "BCBase", ['BCCache', (Cache) ->
Base = (attributes) ->
_constructor = this
_prototype = _constructor.prototype
_constructor.cached = new Cache()
return Base
]
cache.coffee
angular.module('BaseClass')
.factory 'BCCache', ->
Cache = ->
return Cache
规范基本上断言cached 方法(当前)返回一个新的空对象,cache.coffee 文件似乎成功地做到了。但不知何故,Karma 并不认为这两个空对象是等价的。知道为什么吗?我有点难过。
【问题讨论】:
-
2 个对象只有当它们的引用相同时才相等。
-
@PSL 这不是骗子,这是 Jasmine 问题。
toEqual应该在这种情况下工作:groups.google.com/forum/#!topic/jasmine-js/INma5VrV2Xs -
@PSL 不,不是。
toEqual不是===。它遍历一个对象并检查属性。 -
@Sasha 因为第一个对象的构造函数是
Cache而第二个不是。所以它没有达到预期。var aCtor = a.constructor, bCtor = b.constructor; if (aCtor !== bCtor && !(isFunction(aCtor) && (aCtor instanceof aCtor) && isFunction(bCtor) && (bCtor instanceof bCtor))) { return false; }。所以将你的比较设置为expect(Post.cached).toEqual(new Cache()) -
要添加到@PSLs 答案,等于github.com/pivotal/jasmine/blob/master/src/core/matchers/… 的来源显示了所描述的行为。检查第 143 行。请注意,如果您有
expect({}).toEqual({}),那将起作用。错误信息有点误导。
标签: javascript angularjs phantomjs karma-runner karma-jasmine