Angular-Jasmine将一项服务注入测试 [英] Angular-Jasmine injecting a service into the test

查看:91
本文介绍了Angular-Jasmine将一项服务注入测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jasmine的新手,我试图实例化我的控制器,它有一个依赖列表(主要是我写的服务)和我尝试过的所有不同的方法都不对。

New to Jasmine, I am trying to instantiate my controller which has a list of dependencies (mainly the services I have written) and all the different ways I';ve tried haven't been right.

这是我的控制器:

(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 = '';
   .
   .
)();

这是我的测试
(function(){
'use strict' ;

Here is my test (function(){ 'use strict';

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();
        });
    });
  });

})();

以上方式运行测试会出现以下错误:

Running test the above way gives me the following error:

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


推荐答案

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

Try injecting the SearchService like this instead of using 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();
    });
});
});
})();

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

Similarly, you'll have to inject other services as well that your controller is relying on.

这篇关于Angular-Jasmine将一项服务注入测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆