Karma/Jasmine spec -- Expected { } to equal { }
Post.cached
is an instance of Cache
, while your {}
is just a boring ol' Object
. Jasmine considers having a different constructor a valid reason to fail a toEquals
comparison.
If you want to check equality as above, you can do something like:
var mockCache = new Cache();
expect(Post.cached).toEqual(mockCache);
Alternatively, you could just check if it's an empty object:
expect(Object.keys(Post.cached).length).toBe(0);
Thanks to Jeff Storey for the link to the code: https://github.com/pivotal/jasmine/blob/master/src/core/matchers/matchersUtil.js#L143
Sasha
Updated on June 14, 2022Comments
-
Sasha almost 2 years
I'm running a Karma spec to test the functionality of an Angular BaseClass for my models that is outlined in an Egghead.io tutorial.
The behavior seems to be working, but I'm running into a weird error:
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 (it's hard to search, given the characters -- suggests that
toEqual
should be able to recognize the two objects' equivalence -- so I'm a little stumped.Here's the spec code (coffeescript) :
describe 'BCCache', -> it "adds a cache to the model", -> expect(Post.cached).toEqual({})
And here's what it's testing:
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
The spec is basically asserting that the
cached
method (currently) returns a new empty object, which thecache.coffee
file seems to successfully do. But somehow, Karma doesn't see the two empty objects as equivalent. Any idea why? I'm a little stumped. -
SomeKittens over 9 years@PSL Yep, turns out the issue was a misunderstanding of Jasmine's
toEqual
all along. -
PSL over 9 yearsThat is what i commented in the question, way back your answer.. :D, probably i could have answered without commenting and clarifying.. lol.. huff it is a competitive environment.. :D
-
Sasha over 9 yearsThanks everybody! That's an interesting one.
-
PSL over 9 years@sasha Yeah pretty much. But I think this should have been answered by jeff storey or myself... since instead of arguing jasmine is wrong we investigated and pasted relevant parts that led to the answer.