2015-03-06 13 views
24

Tôi đang sử dụng Jasmine để tạo ra một gián điệp như vậy:Làm thế nào để thay đổi giá trị trả lại của điệp viên hoa nhài?

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

Tôi muốn để có thể thay đổi những gì đang được trả về bởi các phương pháp khác nhau của authService.

Dưới đây là cách thức kiểm tra thực tế được thiết lập:

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() 
    }) 
}) 

Dưới đây là cách điều khiển được thiết lập:

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}) 
     }); 
    }]); 

Về cơ bản, tôi muốn để có thể thay đổi giá trị trả về của các phương pháp gián điệp. Tuy nhiên, tôi không chắc liệu tôi có đang tiếp cận vấn đề một cách chính xác hay không bằng cách sử dụng jasmine.createSpyObj.

+0

Phiên bản nào của Jasmine? – sma

+0

Tôi có '' '" hoa nhài-lõi ":" ~ 2.2.0 "' '' trong '' 'package.json''' –

Trả lời

Các vấn đề liên quan