Created
December 19, 2014 12:09
-
-
Save vamshisuram/51799ff9131f71972115 to your computer and use it in GitHub Desktop.
Injecting service(factory) mocks inside other services(factory) in Angularjs-Jasmine tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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