棱角茉莉花将服务注入测试



Jasmine的新手,我正在尝试实例化我的控制器,该控制器具有依赖项列表(主要是我编写的服务),并且我尝试的所有不同方式都不对。

这是我的控制器:

(function () {
'use strict';
angular.module('app.match')
        .controller('MatchController', MatchController);

MatchController.$inject = ['APP_CONFIG', '$authUser', '$http', '$rootScope', '$state', '$stateParams', 'SearchService', 'ConfirmMatchService', 'MusicOpsService', 'ContentOpsService', 'MatchstickService', 'MatchService', 'Restangular'];
function MatchController(APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, searchService, confirmMatchService, musicOpsService, contentOpsService, matchstickService, matchService, Restangular) {
    var vm = this;
    vm.greeting = '';
   .
   .
)();

这是我的测试 (函数(){ "使用严格";

describe('app module', function() {
    var MatchController;
    //beforeEach(module('app.match'));
    beforeEach(function($provide) {
        module = angular.module('app.match');
        $provide.service('SearchService', function(){
        });
    });
    beforeEach(module('app.config'));
    beforeEach(module('auth'));

    beforeEach(inject(function($controller, APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams) {

        MatchController = $controller('MatchController', {'APP_CONFIG':APP_CONFIG, '$authUser':$authUser, '$http':$http, '$rootScope':$rootScope, '$state':$state, '$stateParams':$stateParams, '$provide':$provide});
    }));
    describe("Match controller", function() {
        it("should be created successfully", function() {
            expect(MatchController).toBeDefined();
        });
    });
  });
})();

以上述方式运行测试会给我以下错误:

TypeError: 'undefined' is not a function (evaluating  '$provide.service('SearchService', function(){
            })')

尝试像这样注入SearchService,而不是使用 beforeEach

describe('app module', function() {
var MatchController, SearchService;
beforeEach(module('app.match'));
beforeEach(module('app.config'));
beforeEach(module('auth'));

beforeEach(inject(function($controller, APP_CONFIG, $authUser, $http, $rootScope, $state, $stateParams, _SearchService_) {
    SearchService = _SearchService_;
    MatchController = $controller('MatchController', {
        'APP_CONFIG':APP_CONFIG,
        '$authUser':$authUser,
        '$http':$http,
        '$rootScope':$rootScope,
        '$state':$state,
        '$stateParams':$stateParams,
        '$provide':$provide,
        'SearchService': _SearchService_
    });
}));
describe("Match controller", function() {
    it("should be created successfully", function() {
        expect(MatchController).toBeDefined();
    });
});
});
})();

同样,您还必须注入控制器所依赖的其他服务。

最新更新