2015-03-06 16 views
24

Sto usando Jasmine per creare una spia in questo modo:Come modificare il valore di ritorno della spia jasmine?

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

Mi piacerebbe essere in grado di cambiare ciò che è tornato dai diversi metodi di authService.

Ecco come i test reali sono istituiti:

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

Ecco come il controller è impostato:

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

In sostanza, mi piacerebbe essere in grado di cambiare il valore di ritorno di metodi spiati. Tuttavia, non sono sicuro se sto affrontando correttamente il problema utilizzando jasmine.createSpyObj.

+0

Quale versione di Jasmine? – sma

+0

Ho '' '" jasmine-core ":" ~ 2.2.0 "' '' in '' 'package.json''' –

risposta

Problemi correlati