2015-01-17 5 views
0

나는 내 응용 프로그램에서 다음 컨트롤러가 있습니다단위 테스트 컨트롤러 비웃음 약속 AngularJS와

angular.module('newradarApp').controller('ProfileController', ['$scope', '$log', '$auth', 'userRestService', function ($scope, $log, $auth, userRestService) { 

/** 
* loading ui toggle ativator 
*/ 

$scope.userLoaded = false; 
/** 
* @returns {*} Checks (In combination with satellizer) the contextual authentication state 
*/ 

$scope.userIsLoggedIn = function() { 
    return $auth.isAuthenticated(); 
}; 

//$scope.welcomeMsg = 'Welkom, '; 

/** 
* Holds the instance of the user's service 
*/ 
    var userServiceInstance = userRestService.obtainPersonalInformation().then(function (userData) { 
    $log.debug('::ReST-obtainPersonalInformation Got user data:', JSON.stringify(userData)); 
    $scope.userName = userData.firstName; 
    $scope.fullName = userData.firstName + ' ' + userData.lastName; 
    $scope.picture = encodeURI(userData.profilePicture); 
    $scope.userLoaded = true; 
    }); 


}]); 

내가 재스민이 funtionality을 테스트하고 싶어하고 나는이 방식이 시험을 시도 : 다음

'use strict'; 

describe('Controller: ProfileController', function() { 

// load the controller's module 
beforeEach(module('newradarApp')); 

var ProfileController, scope, mockUserrest, def; 

// Initialize the controller and a mock scope 
beforeEach(inject(function ($controller, $rootScope, $q) { 

mockUserrest = { 
    obtainPersonalInformation: function() { 
    def = $q.defer(); 
    return def.promise; 
} 
}; 

spyOn(mockUserrest, 'obtainPersonalInformation').andCallThrough(); 
scope = $rootScope.$new(); 
ProfileController = $controller('ProfileController', { 
    $scope: scope 
}); 
})); 



it('should assign data to scope', function() { 
def.resolve(userdata); 
scope.$digest(); 
expect(mockUserrest.obtainPersonalInformation).toHaveBeenCalled(); 
expect(scope.userName).toBe(userdata); 
}); 
}); 

을 나는 서비스를 조롱하는 다른 방법을 사용하여이 다른 테스트를 시도했다 :

'use strict'; 

describe('Controller: ProfileController', function() { 

// load the controller's module 
beforeEach(angular.mock.module('newradarApp')); 

var controller, scope, rootScope, mockUserrest, def; 

// Initialize the controller and a mock scope 
beforeEach(inject(function ($controller, $rootScope, $q) { 
rootScope = $rootScope; 
scope = $rootScope.$new(); 
controller = $controller; 

mockUserrest = { 
obtainPersonalInformation: function() { 
def = $q.defer(); 
def.resolve(userdata); 
return def.promise; 
} 
} 


})); 


it('should assign data to scope', function() { 
controller("ProfileController", {$scope: scope, userRestService:   mockUserrest}); 
scope.$digest(); 
expect(scope.username).toBe(userdata) 

}); 
}); 

내가 잘못하고있는 것을 암시 해 주시겠습니까? 귀하가 전화를 기대

답변

0

가 될 것으로 보인다 조금 이상한 당신이 (당신이하지 않는 그것을 조롱가)

'use strict'; 

describe('Controller: ProfileController', function() { 
    var obtainDefer, scope; 
    var $rootScope; 
    beforeEach(angular.module('newradarApp')); 
    beforeEach(inject(function($controller, $rootScope, $q, userRestService) { 
    $rootScope = _$rootScope_; 
    obtainDefer = $q.defer(); 
    scope = $rootScope.$new(); 
    spyOn(userRestService, 'obtainPersonalInformation').and.returnValue(obtainDefer.promise); 
    $controller('ProfileController', {$scope: scope}); 
    })); 
    it('should assign the data from the scope', function() { 
    obtainDefer.resolve({firstName: 'alfred'}); 
    $rootScope.$digest(); 
    expect(scope.userName).toEqual('alfred'); 
    }); 
}); 
전체 서비스를 주입 괜찮다 제공 그래서 여기

expect(scope.userName).toBe(userdata); 

,

+0

'당신은 서비스에 컨트롤러를 건네주었습니다 .' 그것은 OP가 서비스를위한 모의를 설정하기 때문에 명백한 이유로 원래 서비스를 삽입하고 싶지 않기 때문입니다. 그가 갖고있는 것은 unittest에 대한 올바른 접근법입니다. – PSL