最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - KarmaJasmine spec -- Expected { } to equal { } - Stack Overflow

programmeradmin3浏览0评论

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 the cache.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.

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 the cache.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.

Share Improve this question asked Oct 13, 2014 at 22:52 SashaSasha 6,46613 gold badges60 silver badges107 bronze badges 12
  • 2 2 objects are equal only if their references are same. – PSL Commented Oct 13, 2014 at 22:55
  • 3 @PSL This is not a dupe, this is a Jasmine issue. toEqual should work in this instance: groups.google./forum/#!topic/jasmine-js/INma5VrV2Xs – SomeKittens Commented Oct 13, 2014 at 22:59
  • 2 @PSL No, it's not. toEqual is NOT ===. It iterates through an object and checks the properties. – SomeKittens Commented Oct 13, 2014 at 23:01
  • 2 @Sasha Because first object's costructor is Cache and second ones is not. SO it fails the expectation. var aCtor = a.constructor, bCtor = b.constructor; if (aCtor !== bCtor && !(isFunction(aCtor) && (aCtor instanceof aCtor) && isFunction(bCtor) && (bCtor instanceof bCtor))) { return false; }. So set your parison to expect(Post.cached).toEqual(new Cache()) – PSL Commented Oct 13, 2014 at 23:08
  • 2 To add to @PSLs answer, the source of equals github./pivotal/jasmine/blob/master/src/core/matchers/… shows the behavior being described. Check line 143. Note that if you had expect({}).toEqual({}) that would work. It's the error message that is a little misleading. – Jeff Storey Commented Oct 13, 2014 at 23:12
 |  Show 7 more ments

1 Answer 1

Reset to default 4

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 parison.

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./pivotal/jasmine/blob/master/src/core/matchers/matchersUtil.js#L143

发布评论

评论列表(0)

  1. 暂无评论