angularjs - How to provide a service returning different mocked response that is called multiple times -
i trying mock service call gets called multiple times in 1 test, importantly returning different results.
heres example of code i'm testing:
var myarray = []; angular.foreach(docs, function(doc) { myservice.execute().then(function(data) { myarray.push(data); }); )};
and provide unit tests so:
$provide.service('myservice', function() { return { execute: jasmine.createspy('myservice.execute') }; });
so idea can provide mock tests so:
it('should something', function () { myservice.execute.and.callfake(function() { var deferred = $q.defer(); deferred.resolve(mock1); return deferred.promise; }); // expects array have 'mock1' data in
so works singular, when doing more assumed calling again might work tried doing this:
it('should something', function () { myservice.execute.and.callfake(function() { var deferred = $q.defer(); deferred.resolve(mock1); return deferred.promise; }); myservice.execute.and.callfake(function() { var deferred = $q.defer(); deferred.resolve(mock2); return deferred.promise; }); // expected array have 'mock1, mock2' data in
but doesn't work expected returns mock2 twice, can angular.foreach
in test it? i've looked around cant seem find on how achieve want.
Comments
Post a Comment