Orderly Services (worm)

The scenario is very simple, I am trying to test service A, which depends on service B, so I decided to scoff at service B by redefining it before introducing service A. However, I am also testing service B, which is no longer valid because it is overridden. Any idea on how to avoid this?

Application code.

angular.module('MyApp.services')
  .factory('serviceB', function(){
    return {
      greeting: function(){ return 'hello'; }
    }
  })
  .factory('serviceA', ['serviceB', function(serviceB){
    return {
      greeting: function(){
        return 'I say: ' + serviceB.greeting();
      }
  });

Unit Test:

describe('My app services', function(){

  beforeEach(module('MyApp.services'));

  describe('service A', function(){
      // mock service B
    angular.module('MyApp.services').factory('serviceB', function(){
      return { greeting: function(){ return 'mocked B'; } }
    });

    var serviceA;

    inject(function(_serviceA_){
      serviceA = _serviceA_;
    });

    it('should work', function(){
      var words = serviceA.greeting();
      expect(words).toBe('I say: mocked B');          
    });
  });

  describe('service B'. function(){
    var serviceB;

    inject(function(_serviceB_){
      serviceB = _serviceB_;
    });

    it('should work', function(){
      var words = serviceB.greeting();
      expect(words).toBe('hello');
    });
  });
});
+4
source share
2 answers

Your injection of services should probably be either in beforeEachor as part of the tests, otherwise I think they will happen before the start or absence of the tests, for example.

beforeEach(inject(function(_serviceA_){
  serviceA = _serviceA_;
}));

or

it('should work', inject(function(serviceA){
    var words = serviceA.greeting();
    expect(words).toBe('I say: mocked B');          
}));

To solve the problem you are really asking about, I suggest you do one of two things:

  • Cancel the service on the injector, not on the module

    beforeEach(module(function($provide){
      $provide.value('serviceB', {
        greeting: function(){ return 'mocked B'; }
      });
    }));
    

    plunker.

  • , serviceB

    - ( ), , serviceB

    angular.module('MyApp.services.mock', [])
      .factory('serviceB', function(){
        return { greeting: function(){ return 'mocked B'; } }
      });
    

    , , , .

    beforeEach(module('MyApp.services.mock'));
    

    plunker

+10

, , , , (unit test), A, , () B , B .

Unit Test:

describe('My app services', function(){

  beforeEach(module('MyApp.services'));

  describe('service A', function(){
    var serviceA, serviceB;

    inject(function(_serviceA_, _serviceB_){
      serviceA = _serviceA_;
      serviceB = _serviceB_;
    });

    it('should work', function(){
      spyOn(serviceB, 'greeting').and.returnValue('TEST');

      var words = serviceA.greeting();

      expect(words).toBe(I say: TEST');
    });
  });

  describe('service B'. function(){
    var serviceB;

    inject(function(_serviceB_){
      serviceB = _serviceB_;
    });

    it('should work', function(){
      var words = serviceB.greeting();
      expect(words).toBe('hello');
    });
  });
});
0

All Articles