有没有办法巩固'spy '在Jasmine测试中设置
Is there a way to consolidate 'spyOn' setup in Jasmine test?
我正在用Jasmine测试Angular控制器,并监视了近十几个方法。有什么办法可以巩固间谍机构吗?我当前的设置如下:
spyOn(playersService, 'getInfo');
spyOn(playersService, 'getAccounts');
spyOn(playersService, 'getGames');
spyOn(playersService, 'getStatus');
spyOn(playersService, 'getEvents');
spyOn(viewersService, 'getViewers');
spyOn(helpersService, 'formatStats');
spyOn(helpersService, 'formatCounts');
spyOn(helpersService, 'formatValues');
spyOn(PlayerInfoController, 'slideToggle');
spyOn(PlayerInfoController, 'openModal');
Jasmine中没有任何东西允许您批量监视方法。你可以创建自己的。类似于:
function spyOnAll(object) {
var methods = Array.prototype.slice.call(arguments, 1);
if (methods.length) {
for (var i = 0; i < methods.length; i++) {
spyOn(object, methods[i]);
}
}
else {
for (var key in object) {
if (typeof object[key] === "function") {
spyOn(object, key);
}
}
}
}
有两种调用方法。你可以显式地指定对象和方法:
spyOnAll(playerService, "getInfo",
"getAccounts",
"getGames",
"getStatus",
"getEvents",
"getViewers",
"formatStats",
"formatCounts",
"formatValues");
或者监视整个对象:
spyOnAll(playerService);
当然。
function SpyOnInjected(service) {
for (i in arguments) {
spyOn(service, arguments[i]);
}
}
SpyOnInjected(playersService, 'getInfo', 'getAccounts', 'getGames', 'getStatus', 'getEvents');
SpyOnInjected(viewersService, 'getViewers');
SpyOnInjected(helpersService, 'formatStats', 'formatCounts', 'formatValues');
SpyOnInjected(PlayerInfoController, 'slideToggle', 'openModal');
相关文章:
- 如何使用Jasmine测试Knockout.js点击绑定
- 如何在jasmine测试中为有角度的项目注入依赖项
- Jasmine:测试setTimeout函数会抛出一个错误
- 如何通过 phantomjs 可靠地执行利用 requirejs 的 Jasmine 测试
- 使用jasmine测试JavaScript回调函数
- Jasmine测试工厂函数调用本地函数和另一个函数-获取错误:应该是间谍,但得到了function
- 用jasmine测试JavaScriptUI的最佳方法
- 如何使用jasmine测试done和failedDeferredObject
- 在CoffeeScript文件上运行Jasmine测试时,对象未定义错误
- 使用 Jasmine 测试主干视图时,浏览器页面不断刷新
- 使用 Jasmine 测试对象中的事件处理程序
- 在 Jasmine 测试中无法访问 Angular 服务函数
- 如何正确引用Jasmine测试夹具中的图像
- jasmine测试抱怨undefined不是一个对象
- 我怎么能用jasmine测试这个代码呢
- 如何使用jasmine测试ajax响应
- 如何模拟ajax对Jasmine测试的调用
- AngularJS Jasmine测试:类型错误:'未定义'不是对象
- Jasmine测试url的javascript函数
- 使用Jasmine测试TypeScript类