【问题标题】:Unit Testing Controller Mocking Promises Angularjs单元测试控制器模拟承诺 Angularjs
【发布时间】:2015-03-15 17:16:51
【问题描述】:

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

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)

});
});

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

【问题讨论】:

    标签: angularjs unit-testing controller mocking


    【解决方案1】:

    你的期望调用似乎有点奇怪

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

    【讨论】:

    • You were passing in the service to the controller 那是因为OP正在为服务设置mock,他不想注入原始服务,原因很明显。他所拥有的是正确的单元测试方法。
    猜你喜欢
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 2014-05-14
    • 2013-09-25
    • 2016-07-15
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多