javascript Jasmine call.length 和 callCount 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20972533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jasmine calls.length and callCount are undefined
提问by Dru
I'm trying to test my chrome extension with Jasminebut I'm having trouble getting calls.length
and callCount
to behave as expected. Both cases return undefined
.
我想测试我的浏览器扩展程序与茉莉,但我遇到了麻烦calls.length
,并callCount
表现为预期的。两种情况都返回undefined
。
I've included a sample of the code and the spec. Here's the rest of the code if it helps: https://github.com/DruRly/kamikaze/tree/closeIdleTab
我已经包含了代码和规范的示例。如果有帮助,这里是其余的代码:https: //github.com/DruRly/kamikaze/tree/closeIdleTab
How to reproduce:
如何重现:
git clone https://github.com/DruRly/kamikaze/tree/closeIdleTab
cd kamikaze
open SpecRunner.html
git clone https://github.com/DruRly/kamikaze/tree/closeIdleTab
cd kamikaze
open SpecRunner.html
spec/kamikazeSpec.js
规格/kamikazeSpec.js
describe("kamikaze", function() {
describe("closeIdleTabs", function(){
it("calls closeIdleTab for each tab received", function(){
spyOn(kamikaze, 'closeIdleTab');
kamikaze.closeIdleTabs([1,2,3]);
expect(kamikaze.closeIdleTab.calls.length).toBe(3);
})
})
})
src/kamikaze.js
源代码/神风.js
kamikaze = {
...
closeIdleTabs: function(tabs){
tabs.forEach(function(tab){
test.closeIdleTab(tab);
})
},
closeIdleTab: function(tab){
if(tabTimeStamps[tab.id]){
var secondsSinceUpdated = getSecondsSinceUpdated(tab.id)
if(secondsSinceUpdated > (minutesUntilIdle * 60)){
chrome.tabs.remove(tab.id)
}
}
},
...
}
回答by gkalpak
The Jasmine APIs have changed a bit in the 2.x version "series".
According to the latest docsyou should use the count()
method:
Jasmine API 在 2.x 版本“系列”中发生了一些变化。
根据最新的文档,您应该使用该count()
方法:
expect(kamikaze.closeIdleTab.calls.count()).toBe(3);
I also tried that with your code and all tests pass successfully.
我也用你的代码尝试过,所有测试都成功通过。
回答by HolgerJeromin
Alternative syntax (with a bit nicer output on failure) would be
替代语法(失败时输出更好)将是
expect(kamikaze.closeIdleTab).toHaveBeenCalledTimes(3);