angularjs – 如何改变茉莉花间谍的回报值?
发布时间:2020-12-17 07:44:23 所属栏目:安全 来源:网络整理
导读:我正在使用茉莉花创建一个这样的间谍: beforeEach(inject(function ($injector) { $rootScope = $injector.get('$rootScope'); $state = $injector.get('$state'); $controller = $injector.get('$controller'); socket = new sockMock($rootScope); //this
我正在使用茉莉花创建一个这样的间谍:
beforeEach(inject(function ($injector) { $rootScope = $injector.get('$rootScope'); $state = $injector.get('$state'); $controller = $injector.get('$controller'); socket = new sockMock($rootScope); //this is the line of interest authService = jasmine.createSpyObj('authService',['login','logout','currentUser']); })); 我想要更改authService的各种方法返回的内容. 以下是实际测试的设置: function createController() { return $controller('UserMatchingController',{'$scope': $rootScope,'socket':socket,'authService': authService }); } describe('on initialization',function(){ it('socket should emit a match',function() { createController(); expect(socket.emits['match'].length).toBe(1); }); it('should transition to users.matched upon receiving matched',function(){ //this line fails with "TypeError: undefined is not a function" authService.currentUser.andReturn('bob'); createController(); $state.expectTransitionTo('users.matched'); socket.receive('matchedblah',{name: 'name'}); expect(authService.currentUser).toHaveBeenCalled() }) }) 控制器的设置方式如下: lunchrControllers.controller('UserMatchingController',['$state','socket','authService',function ($state,socket,authService) { socket.emit('match',{user: authService.currentUser()}); socket.on('matched' + authService.currentUser(),function (data) { $state.go('users.matched',{name: data.name}) }); }]); 从本质上讲,我希望能够更改间谍方法的返回值.但是,我不知道如果我正在使用jasmine.createSpyObj正确地解决了这个问题.
试试这个. Jasmine 2.0的API已更改:
authService.currentUser.and.returnValue('bob'); 文档: http://jasmine.github.io/2.0/introduction.html#section-Spies (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |