AngularJS单元测试-无法将简单服务注入控制器

时间:2018-07-11 11:22:48

标签: javascript angularjs jasmine

我有一个简单的单元测试。

我正在尝试注入服务SearchPageService,但我得到

  

未知提供者:SearchPageServiceProvider <-SearchPageService

我在karma.conf上配置了文件

  files: [
    'angular.js'
    '../app/app.js',
    '../components/angular-mocks/angular-mocks.js',
    'unit/*.js',
    '../app/src/callCenter/page1/searchPageService.js',
    '../app/src/callCenter/page1/searchPageController.js'
],

测试

describe('Controller: testController', function() {

beforeEach(function() {
        angular.module('moduleApp', ['moduleApp.moduleDependencies', 'angular.dependencies', 'third.party.dependencies', 'ipp.dependencies']);
        angular.module('moduleApp.controller');
        angular.module('moduleApp.services');   
    }
);

var ctrl;
var $scope;
var service;

beforeEach(inject(function($controller,$rootScope, SearchPageService,$http){
    console.log('*** IN INJECT!!***: ', SearchPageService);

    $scope = $rootScope.$new();
    service = SearchPageService;

    ctrl = $controller(SearchPageController, {
        $scope: $scope,
        SearchPageService: service,
        $http:$http
    });
}));


it('should have $scope defined', function() {
    expect($scope).toBeDefined();
});

});

控制器

    angular.module('moduleApp.controller').controller('moduleApp.SearchPageController', SearchPageController);
SearchPageController.$inject = ['$log', '$scope', 'moduleApp.SearchPageService', '$http'];


function SearchPageController($log, $scope, searchPageService, $http) { //code }

服务:

 angular.module('moduleApp.services').service('moduleApp.SearchPageService', SearchPageService);
SearchPageService.$inject = ['$log', 'AnotherCustomService'];

function SearchPageService ($log, anotherCustomService) { //code }

0 个答案:

没有答案