热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在茉莉花测试中是否有一种巩固“spyOn”设置的方法?-Isthereawaytoconsolidate'spyOn'setupinJasminetest?

ImdoingsometestingofAngularcontrollerswithJasmineandspyingonalmostadozenmethods.Is

I'm doing some testing of Angular controllers with Jasmine and spying on almost a dozen methods. Is there any way to consolidate the spy setup? My current setup looks like:

我正在用茉莉花做一些角度控制器的测试,并对近十几种方法进行监视。有没有办法巩固间谍机构?我当前的设置如下:

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');

This just strikes me as a lot of repeated code.

我觉得这是很多重复的代码。

2 个解决方案

#1


1  

There is nothing in Jasmine that allows you to spy on methods in bulk. You could create your own. Something akin to:

《茉莉花》中没有任何东西可以让你大量监视方法。你可以自己创造。类似于:

function spyOnAll(object) {
    var methods = Array.prototype.slice.call(arguments, 1);

    if (methods.length) {
        for (var i = 0; i 

You have two ways to call it. You can specify the object and methods explicitly:

有两种说法。您可以明确指定对象和方法:

spyOnAll(playerService, "getInfo",
                        "getAccounts",
                        "getGames",
                        "getStatus",
                        "getEvents",
                        "getViewers",
                        "formatStats",
                        "formatCounts",
                        "formatValues");

Or spy on the whole object:

或者监视整个对象:

spyOnAll(playerService);

#2


1  

Of course.

当然可以。

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');

推荐阅读
author-avatar
卢健波_785
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有