1

I'm trying to inject a service into my controllers unit test. I'm not looking to make a mock out of the service but just include it so that I can have access to all of its methods. Is this possible?

'use strict';

  describe('Offers : controller', function(){

    beforeEach(module('client'));

      var $scope,
          controller,
          $rootScope,
          genericService;


      beforeEach(inject(function($injector, $controller) {

        $rootScope = $injector.get('$rootScope');
        controller = $controller('OffersCtrl', { $scope: $scope });
        genericService = $injector.get('genericService');
        $scope = $rootScope.$new();

   }));

   it('should have a genericService', function () {
     expect(genericService.method).toBeDefined();
   });


});

1 Answer 1

2

Just inject your own service the same way you did with the angular services:

var genericService;

beforeEach(inject(function(_genericService_) {
  genericService = _genericService_;
});

Note that you can use underscores around the argument name to avoid naming conflicts. Angular will strip those from the argument name when resolving the services to inject.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.