2016-12-15 12 views
0

これはジャスミンテストを使用してカバーしようとしているコントローラとサービスです。ここで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);  
    }); 
}); 

事がときということです私は、 "注射"スニペット、thを取り除いていますeテストはうまくいく。注入されるとすぐに、問題が発生します。

エラーは以下の通りされています

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 
     [email protected]:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:321:24 
     [email protected]:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:4601:12 
     [email protected]:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:4523:30 
     [email protected]:/FileVault/file-vault/filevault-ui/static/scripts/lib-min.js:50216:60 
     [email protected]:/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

をあなたは_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); 
    }); 
}); 
関連する問題