When all components are contained within a single module A.
angular.module('A',[])
.service('B', function(D) { // Utilizing Dependency Injection to inject service D
this.C = function() {
return !D.E();
}
return this;
})
.service('D', function() {
this.E = function() { return false; };
return this;
});
Unit Testing:
describe('Unit Test for the Service B', function() {
var B, D;
beforeEach(function() {
module('A');
});
beforeEach(inject(function(_B_, _D_) {
B = _B_;
D = _D_;
}));
describe('Functionality of method C', function() {
it('should invert the return value of method E in service D', function() {
var E = D.E();
var C = B.C();
expect(E).toBeFalsy();
expect(C).toBeTruthy();
});
});
});
Now, assuming it resides in a different module - module Z.
angular.module('A',['Z']) // Including module Z
.service('B', function(D) { // Injecting service D using Dependency Injection
this.C = function() {
return !D.E();
}
return this;
});
angular.module('Z', [])
.service('D', function() {
this.E = function() { return false; };
return this;
});
Unit Tests:
describe('Unit Test for the Service B', function() {
var B, D;
beforeEach(function() {
module('A');
module('Z');
});
beforeEach(inject(function(_B_, _D_) {
B = _B_;
D = _D_;
}));
describe('Functionality of method C', function() {
it('should invert the return value of method E in service D', function() {
var E = D.E();
var C = B.C();
expect(E).toBeFalsy();
expect(C).toBeTruthy();
});
});
});