2015-03-06 16 views
24

Ben şöyle bir casus oluşturmak için Jasmine'i kullanıyorum:Yasemin casusunun dönüş değeri nasıl değiştirilir?

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

Ben authService çeşitli yöntemlerle döndürülen ne değiştirmek mümkün istiyorum. Ben dönüş değerini değiştirmek edebilmek istiyorum,

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

Esasen:

İşte
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() 
    }) 
}) 

kontrolör kurulur nasıl: Burada

fiili testler nasıl ayarlanır vardır casus yöntemlerin Ancak, jasmine.createSpyObj kullanarak sorunu doğru bir şekilde yaklaşıyor muyum emin değilim.

+0

sürümü nedir? – sma

+0

"Yasemin" temelim var: "~ 2.2.0" '' '' 'package.json''' –

cevap