How to provide a service returning different mocked response that is called multiple times
<p>I am trying to successfully mock a service call which gets called multiple times in one test, importantly returning different results. </p>

<p>heres an example of code i'm testing:</p>

<pre><code> var myArray = [];
 angular.forEach(docs, function(doc) {
     MyService.execute().then(function(data) {
          myArray.push(data);
     });
 )};
</code></pre>

<p>and then i provide to my unit tests like so:</p>

<pre><code>$provide.service('MyService', function() {
    return {
        execute: jasmine.createSpy('MyService.execute')
    };
});
</code></pre>

<p>so then the idea is i can provide a mock for my tests like so:</p>

<pre><code>it('should do something', function () {

    MyService.execute.and.callFake(function() {
        var deferred = $q.defer();
        deferred.resolve(mock1);
        return deferred.promise;
    });

    // expects my array to have 'mock1' data in 
</code></pre>

<p>so this works for a singular, but when doing more than i assumed just calling it again might work so i tried doing this:</p>

<pre><code>it('should do 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 my array to have 'mock1, mock2' data in 
</code></pre>

<p>but doesn't work as expected it just returns the mock2 twice, can i do an <code>angular.forEach</code> in my test for it? i've looked around but cant seem to find anything on how to achieve what i want.</p>