noobcoder
noobcoder

Reputation: 12861

Angular-Jasmine injecting a service into the test

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.

Here is my controller:

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

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(){
            })')

Upvotes: 1

Views: 7456

Answers (1)

SiddAjmera
SiddAjmera

Reputation: 39482

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.

Upvotes: 2

Related Questions