Jasmine单元测试用例在注入依赖项时失败

时间:2016-12-15 06:42:45

标签: angularjs unit-testing karma-jasmine

这些是我试图用Jasmine测试覆盖的控制器和服务。

home.controller.js

var home = angular.module('home',[])

home.controller('home.controller', ['$scope','$location','homeService','localStorageService',function ($scope,$location,homeService,localStorageService) {
    homeService.directories(1001).then(function mySucces(_data) {
    $scope.dataHome = _data;
    }, function myError(response) {
         alert("error");

        });

     $scope.func = function(fileData) {
     localStorageService.set('fileId',fileData.id);
     console.log(fileData.id);
     $location.path("fileview");
        };

    }]);

home.service.js

var homeService = angular.module('homeService',[]); 

  homeService.factory('homeService',['$http', '$q', '$log','config', function ($http, $q, $log,config) {
    var serviceAPI = {};
    serviceAPI.directories = function (folderId) {
    var deferred = $q.defer();
    $http.get(config.service + "/filevault-service/folders").then(function mySucces(response) {
    deferred.resolve(response.data.data);
        }, function myError(response) {
         deferred.resolve(response.status);
        });
    return deferred.promise;
    }


    return serviceAPI;

    }]);

这是我的测试用例:

describe('equality', function() {

  var rootScope, homeService, scope, homeController, httpBackend, location, localStorageService;

beforeEach(function(){

  module('fileVaultApp');

  inject(function($controller, $httpBackend, $rootScope, $location, homeService, localStorageService){
      rootScope = $rootScope;
      scope = $rootScope.$new();
      httpBackend = $httpBackend;
      homeService = homeService;
      location = $location;
      localStorageService = localStorageService;
      homeController = $controller('home.controller', { $scope: scope });
      spyOn($location, 'path');
  });

});

console.log("hello");     
it('check the home controller', function() {      
      expect(1).toBe(1);      
     });
});

问题是,当我删除“注入”片段时,测试运行良好。一旦注入,就会出现问题。

错误如下:

PhantomJS 2.1.1 (Windows 7 0.0.0) equality check the home controller FAILED
        C:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:4641:53
        forEach@C:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:321:24
        loadModules@C:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:4601:12
        createInjector@C:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:4523:30
        workFn@C:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:50216:60
        inject@C:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:50196:46
        C:/FileVault/file-vault/filevault-ui/tests/test_jasmine_spec.js:7:12
PhantomJS 2.1.1 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) (0 secs / 0.012 secs)

1 个答案:

答案 0 :(得分:1)

您应该注入_homeService__localStorageService_;

inject(function($controller, $httpBackend, $rootScope, $location, _homeService_, _localStorageService_){

此外,您应该在实例化时注入控制器的所有依赖项。

describe('equality', function() {
    var rootScope, homeService, scope, homeController, httpBackend, location, localStorageService;
    beforeEach(function(){
      module('fileVaultApp');
      inject(function($controller, $httpBackend, $rootScope, $location, _homeService_, _localStorageService_){
         rootScope = $rootScope;
         scope = $rootScope.$new();
         httpBackend = $httpBackend;
         homeService = _homeService_;
         location = $location;
         localStorageService = _localStorageService_;
         homeController = $controller('home.controller', { $scope: scope, $location:location, homeService: homeService, localStorageService: localStorageService});
         spyOn($location, 'path');
     });
   });
   console.log("hello");
   it('check the home controller', function() {
       expect(1).toBe(1);
     });
});