单元测试控制器模拟承诺Angularjs

时间:2015-01-17 01:19:51

标签: angularjs unit-testing controller mocking

我的应用程序中有以下控制器:

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


}]);

我想用Jasmine测试这个功能,我用这种方式尝试了这个测试:

'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)

});
});

每个人都没有通过,所以暗示我做错了什么?

1 个答案:

答案 0 :(得分:0)

你期待的电话似乎有点奇怪

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

所以在这里,如果你不介意注入整个服务(你不 来嘲笑它)

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