Jasmine,Angular;rootScope$广播“;测验
Jasmine, Angular "rootScope.$broadcast" test
我正试图为具有$rootScope.$on('accountsSet', function (event)...
的控制器编写一个测试。所以在测试中,我使用了.broadcast.andCallThrough()
,这是So中的许多其他问题建议的,而它以前也适用于我
所以我的控制器很简单:
角度模块("控制器侧菜单",[])
.controller('SidemenuCtrl', function($rootScope, $scope, AccountsService) {
$rootScope.$on('accountsSet', function (event) {
$scope.accounts = AccountsService.getAccounts();
$scope.pro = AccountsService.getPro();
});
});
任何测试都很简单:
describe("Testing the SidemenuCtrl.", function () {
var scope, createController, accountsService;
beforeEach(function(){
angular.mock.module('trevor');
angular.mock.module('templates');
inject(function ($injector, AccountsService) {
scope = $injector.get('$rootScope');
controller = $injector.get('$controller');
accountsService = AccountsService;
createController = function() {
return controller('SidemenuCtrl', {
'$scope' : $injector.get('$rootScope'),
'AccountsService' : accountsService,
});
};
});
});
it("Should load the SidemenuCtrl.", function () {
accountsService.setPro(true);
spyOn(scope, '$broadcast').andCallThrough();
var controller = createController();
scope.$broadcast("accountsSet", true);
expect(scope.pro).toBeTruthy();
});
});
对于spyOn(scope, '$broadcast').andCallThrough();
,我得到的错误。注意,这个测试的范围是rootScope
,所以这应该不是问题。
因此,引用该行的错误:TypeError:"undefined"不是函数(正在计算"spyOn(scope,'$broadcast').andCallThrough()")在/tests/controllers/sidemenu.js:30
我要把我的评论变成一个答案,因为它原来是解决方案:
在jasmine 2.0中,间谍的语法发生了变化(还有许多其他内容,请参阅此处的漂亮文档)新语法是
spyOn(foo, 'getBar').and.callThrough();
与:的jasmine 1.3语法进行比较
spyOn(foo, 'getBar').andCallThrough();
相关文章:
- 使用Javascript创建测验页面
- 在 单击将广播更改为选中或未选中
- 套接字服务器向客户端广播,但不从两者接收
- 在侦听特定端口时接收广播的数据包
- chrome.socket如何用于广播或多播
- Jasmine,Angular;rootScope$广播“;测验
- 奇怪的对象测验-JavaScript-初学者
- 为测验应用程序洗牌2个以上的数组
- $routeUpdate未在角因果报应单元测试中广播
- 超级棘手的Javascript测验,需要弄清楚答案
- 添加测验计时器,如果计时器达到0,则淡出/跳到下一个
- websocketrails无法在浏览器中接收广播响应
- 有关如何安全地记录用户回答测验挑战所需的时间的想法
- 指令中未收到广播
- 如何将路径更改广播到 AngularJS 应用程序中的所有视图
- 用于在 SCORM1.2 中计算测验分数的 Javascript
- 我的测验中的JavaScript计算器
- 广播消息 (ping) 交叉颜色扩展
- 推送器:无法广播客户端事件
- 指定的侧边栏广播器无效 -- 有效的侧边栏广播器是什么样子的