Skip to content

Instantly share code, notes, and snippets.

@hcabnettek
Created June 14, 2013 08:46
Show Gist options
  • Save hcabnettek/5780431 to your computer and use it in GitHub Desktop.
Save hcabnettek/5780431 to your computer and use it in GitHub Desktop.
how can I unit test Service2 which depends on Service1? I keep getting a Service1Provider not found error.
!function(ng){
'use strict';
var module = ng.module('foo.services', []);
(function($ng, $module) {
function Service($q) {
return {
bar: function(a,b,c){
var baz = a+b+c;
return function(d,e,f){
var deferred = $q.defer();
if(baz > 0){
deferred.resolve({result: baz + d + e + f });
} else {
deferred.reject({ err: 'baz was <= 0'})
}
return deferred.promise;
}
}
};
}
$module.factory("Service1", ['$q', Service]);
})(ng, module);
(function($ng, $module) {
function Service(Service1) {
function doSomething(){
var result;
var whatever = Service1.bar(5,6,7);
var promise = whatever(8,9,10);
promise.then(function(data){
result = data.result;
//data.result should be 45 here
}, function(err){
});
return result;
}
return {
bam:doSomething
};
}
$module.factory("Service2", ["Service1", Service]);
})(ng, module);
}(angular);
var myApp = angular.module('myApp',['foo.services']);
describe('foo service tests', function () {
beforeEach(function () {
module('foo.services');
});
//http://jsfiddle.net/hcabnettek/JJ9Rk/
it('bam should return 45', inject(function (Service2) {
var expectation = Service1.bam();
expect(expectation).toBe(45);
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment