Skip to content

Instantly share code, notes, and snippets.

@vamshisuram
Created December 19, 2014 12:09
Show Gist options
  • Save vamshisuram/51799ff9131f71972115 to your computer and use it in GitHub Desktop.
Save vamshisuram/51799ff9131f71972115 to your computer and use it in GitHub Desktop.
Injecting service(factory) mocks inside other services(factory) in Angularjs-Jasmine tests
// Feel free to mention if something is wrong. :)
describe('Service-1 tests', function() {
var Service1, Service2;
var Service2-Mock = {
// some object data/methods
};
beforeEach(function () {
module('servicesmodule');
module(function ($provide) {
$provide.value('Service2', Service2-Mock);
});
// Now when angular fetches Service2 it gets `Service2-Mock`.
inject(function (_Service1_, _Service2_) {
Service1 = _Service1_;
Service2 = _Service2_;
});
});
it('Service1 functions test', function () {
// sampleFn in Service1 that uses Service2 function
// function sampleFn() {
// Service2.getData()
// ...
// }
Service1.sampleFn();
// add expectations
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment