2016-12-15 70 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); 
    }); 
});