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(){
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');
});
});
});
Rn2dy source
share