Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Karma/Jasmine规范--预期{}等于{}_Javascript_Angularjs_Phantomjs_Karma Runner_Karma Jasmine - Fatal编程技术网

Javascript Karma/Jasmine规范--预期{}等于{}

Javascript Karma/Jasmine规范--预期{}等于{},javascript,angularjs,phantomjs,karma-runner,karma-jasmine,Javascript,Angularjs,Phantomjs,Karma Runner,Karma Jasmine,我正在运行一个Karma规范来测试Egghead.io教程中概述的模型的Angular基类的功能 这种行为似乎有效,但我遇到了一个奇怪的错误: PhantomJS 1.9.7 (Mac OS X) BCCache adds a cache to the model FAILED Expected { } to equal { }. Error: Expected { } to equal { }. (考虑到这些字符,很难搜索,toEqual应该能够识别这两个对象的等价性,所以我

我正在运行一个Karma规范来测试Egghead.io教程中概述的模型的Angular基类的功能

这种行为似乎有效,但我遇到了一个奇怪的错误:

PhantomJS 1.9.7 (Mac OS X) BCCache adds a cache to the model FAILED
  Expected {  } to equal {  }.
  Error: Expected {  } to equal {  }.
(考虑到这些字符,很难搜索,
toEqual
应该能够识别这两个对象的等价性,所以我有点困惑

以下是规范代码(coffeescript):

下面是它正在测试的内容:

基础咖啡

angular.module("BaseClass")
  .factory "BCBase", ['BCCache', (Cache) ->
    Base = (attributes) ->
      _constructor = this
      _prototype = _constructor.prototype

      _constructor.cached = new Cache()

    return Base
  ]
angular.module('BaseClass')
  .factory 'BCCache', -> 
    Cache = ->    
    return Cache
缓存。咖啡

angular.module("BaseClass")
  .factory "BCBase", ['BCCache', (Cache) ->
    Base = (attributes) ->
      _constructor = this
      _prototype = _constructor.prototype

      _constructor.cached = new Cache()

    return Base
  ]
angular.module('BaseClass')
  .factory 'BCCache', -> 
    Cache = ->    
    return Cache

规范基本上是断言
缓存的
方法(当前)返回一个新的空对象,
cache.coffee
文件似乎成功地实现了这一点。但不知何故,Karma并不认为这两个空对象是等效的。知道为什么吗?我有点困惑。

Post.cached
缓存的一个实例,而您的
{}
只是一个无聊的ol'
对象
。Jasmine认为拥有不同的构造函数是导致
toEquals
比较失败的有效原因

如果要如上所述检查相等性,可以执行以下操作:

var mockCache = new Cache();
expect(Post.cached).toEqual(mockCache);
或者,您可以检查它是否为空对象:

expect(Object.keys(Post.cached).length).toBe(0);

感谢Jeff Storey提供的代码链接:

2个对象只有在它们的引用相同时才是相等的。@PSL这不是一个重复,这是一个Jasmine问题。
toEqual
应该在这个实例中起作用:@PSL不,不是。
toEqual
不是
==
。它迭代对象并检查属性。@Sasha因为第一个对象的构造函数是
Cache
,而第二个对象的构造函数不是。因此它没有达到预期效果。
var aCtor=a.constructor,bCtor=b.constructor;if(aCtor!==bCtor&!(isFunction(aCtor)&(aCtor instanceof aCtor)&&isFunction(bCtor)&(bCtor instanceof bCtor)){返回false;}
。因此,将比较设置为
expect(Post.cached).toEqual(new Cache())
要添加到@PSLs answer,equals的源显示所描述的行为。检查第143行。注意,如果您有
expect({})。toEqual({})
那就行了。错误信息有点误导。@PSL是的,原来问题一直是对Jasmine的
toEqual的误解。这就是我在问题中所评论的,回到你的答案上来….:D,也许我可以不加评论和澄清就回答..哈哈..这是一个竞争环境nt.:谢谢大家!这是一个有趣的问题。@sasha是的,非常有趣。但我认为这应该由jeff storey或我自己来回答……因为我们没有争辩jasmine是错的,而是调查并粘贴了导致答案的相关部分。