Karma - 使用Mocked API对角度控制器进行单元测试

时间:2017-08-03 00:51:43

标签: angularjs unit-testing karma-jasmine

我为一个角度工厂编写了一些单元测试,使用$ q.resolve({})模拟带有假承诺的响应。我现在正在编写控制器测试,并希望从控制器调用工厂并将已解析的模拟工厂响应分配给控制器范围(vm),并验证数据。这是从控制器内测试工厂的正确方法吗?

出厂测试

describe('getVICs()', function() {
  var vicFactory, $q, $httpBackend, result;
  beforeEach(module('vicModule'));
  beforeEach(inject(function(_vicFactory_, _$q_, _$httpBackend_) {
    vicFactory = _vicFactory_;
    $q = _$q_;
    $httpBackend = _$httpBackend_;
  }));
  beforeEach(function() {
    result = {};
    spyOn(vicFactory, 'getVICs').and.callThrough(function() {
      var deferred = $q.defer();
      return deferred.promise;
    });
  });
  it('should be defined', function() {
    expect(vicFactory.getVICs()).toBeDefined();
  });
  it('should return list of vics', function() {
    var vicList = {
      "results": [
        {
          "version": 5
        },
        {
          "version": 1
        }
      ]
    };
    var qs = '?sort=version&dir=DESC&page=1&limit=10&status=ACTIVE';
    $httpBackend.expectGET('/path/to/my/api' + qs).respond(200, vicList);
    $httpBackend.whenGET('http://localhost:3000/path/to/my/api' + qs).respond(200, $q.when(vicList));
    vicFactory.getVICs(qs)
    .then(function(response) {
      result = response;
    });
    // flush pending HTTP requests
    $httpBackend.flush();
    expect(result.data.results).toBeDefined();
  });
});

工厂

function getVICs(query) {
  return $http.get('/path/to/my/api' + query)
  .then(function(response) {
    return response;
  });
}

控制器测试

describe('vicController', function() {
  var $controller, vicController, vicFactory, $q, $scope, $httpBackend, deferred;
  var vicList = {
    "results": [
      {
        "version": 5
      },
      {
        "version": 1
      }
    ]
  };
  var qs = '?sort=version&dir=DESC&page=1&limit=10&status=ACTIVE';
  beforeEach(module('vicModule'));
  beforeEach(inject(function(_$controller_, _vicFactory_, _$q_, _$rootScope_, _$httpBackend_, _$httpParamSerializer_) {
    $controller = _$controller_;
    vicFactory = {
      getVICs: function () {
        deferred = $q.defer();
        return deferred.promise;
      }
    };
    $q = _$q_;
    $rootScope = _$rootScope_;
    $scope = $rootScope.$new();
    $httpBackend = _$httpBackend_;
    $httpParamSerializer = _$httpParamSerializer_;
    spyOn(vicFactory, 'getVICs').and.callThrough();
    vicController = $controller('vicController', {vicFactory: vicFactory});
    deferred.resolve(vicList);
  }));
  it('should be defined', function() {
    expect(vicController).toBeDefined();
  });
  it('should call getVICs() on load', function() {
    $httpBackend.expectGET(''/path/to/my/api?' + qs).respond(200, vicList);
    $scope.$digest();
    expect(vicFactory.getVICs).toHaveBeenCalledWith(qs);
    expect($scope.vicList).toEqual(vicList);
  });
});

控制器

  angular.module('vicModule')
  .controller('vicController', vicController);
  vicController.$inject = [
    'vicFactory'
  ];

  function vicController(vicFactory) {
    var vm = this;
    var qs = '?sort=version&dir=DESC&page=1&limit=10&status=ACTIVE';
    vicFactory.getVICs(qs)
    .then(function(response) {
      console.log('vic controller success');
      console.log(response);
      vm.vicList = response;
    }, function(response) {
      console.log('vic controller error');
      console.log(response);
    });
  }

固定

通过注入$ q和$ scope,在beforeEach中设置vicFactory,更新spy,然后在控制器初始化后使用预期的数据集解析延迟来完成此工作。控制器测试规范已经更新,希望对某人有所帮助。

0 个答案:

没有答案