title: multiple get requests one controller: Unexpected request, unit testing - AngularJS
body: <p>I can find plenty of examples of single http calls from a controller and how to test them,but no examples of multiple testing.</p>
  
  <p>My first test works fine without <code>Product.find(10)</code> in the controller. When I add that line however the first test collapses.</p>
  
  <p>The errors: </p>
  
  <p><code>Error: Unexpected request: GET 0.0.0.0:3000/api/products
      No more request expected
  </code></p>
  
  <p>and</p>
  
  <p><code>Error: Unexpected request: GET 0.0.0.0:3000/api/products
      No more request expected
  </code></p>
  
  <p>I've tried a number of things: including both in the before each, this gave me an undefined error, i tried using expect instead of when, I tried adding both whens to both tests, and a combination of the above. I'm clearly doing something very wrong but being an angular newbie, it's hard to work out exactly what that might be, especially with the lack of examples.. I am just looking to get my first test to pass with <code>Product.find(10)</code></p>
  
  <p>Here are my tests:</p>
  
  <pre><code>'use strict';
  
  describe('productsController', function() {
  
      var scope, $httpBackend;
      var api_root = '0.0.0.0:3000/api/';
  
      beforeEach(angular.mock.module('sprangularApp'));
  
      beforeEach(angular.mock.inject( function($rootScope, $controller, _$httpBackend_) {
  
          $httpBackend = _$httpBackend_;
  
          //Get mock jsons
          jasmine.getJSONFixtures().fixturesPath='base/js/tests/api_mock';
  
          scope = $rootScope.$new();
          $controller('productsController', {$scope: scope});
      }));
  
      //Start Tests
      it('Should be array of all products', function() {
          $httpBackend.when('GET', api_root + 'products').respond(
              getJSONFixture('products.json')
          );
  
          $httpBackend.flush();
          expect(scope.products[3].name).toBe('Ruby on Rails Bag');
      });
  
      it('Should instantiate a new product object from json data', function() {
          $httpBackend.when('GET', api_root + 'products/10').respond(
              getJSONFixture('10.json')
          );
  
          $httpBackend.flush();
          expect(scope.currentProduct.name).toBe('Spree Ringer T-Shirt');
      });
  }); 
  </code></pre>
  
  <p>my controller that I am testing:</p>
  
  <pre><code>// Generated by CoffeeScript 1.6.3
  (function() {
    var sprangularControllers;
  
    sprangularControllers = angular.module('sprangularControllers', ['sprangularServices']);
  
    sprangularControllers.controller('productsController', [
      '$scope', 'Product', function($scope, Product) {
        Product.products_with_meta().$promise.then(function(response) {
          return $scope.products = response.products;
        });
        return Product.find(10);
      }
    ]);
  
  }).call(this);
  </code></pre>
  
  <p>And the factory with the resource requests:</p>
  
  <pre><code>sprangularServices = angular.module('sprangularServices', ['ngResource'])
  
  sprangularServices.factory('Defaults', -&gt; 
      api_url: "0.0.0.0:3000/api/"
  )
  
  sprangularServices.factory('Product', ($resource, Defaults) -&gt; 
      # $resource(Defaults.api_url + 'products.json')
      class Product
          constructor: -&gt;
              @service = $resource(Defaults.api_url + 'products/:id', {id: '@id'})
  
          this.products_with_meta = -&gt;
              service = $resource(Defaults.api_url + 'products')
              service.get()
  
          this.find = (id) -&gt; 
              service = $resource(Defaults.api_url + 'products/:id', {id: id})
              service.get()
  )
  </code></pre>
  
  <p>As per michael's suggestion I have edited my test to this, however I am still getting the exact same result:</p>
  
  <p>'use strict';</p>
  
  <pre><code>describe('productsController', function() {
  
      var $rootScope, $httpBackend, createController;
      var api_root = '0.0.0.0:3000/api/';
  
      beforeEach(angular.mock.module('sprangularApp'));
  
      beforeEach(inject(function($injector) {
          $httpBackend = $injector.get('$httpBackend');
  
          //Get mock jsons
          jasmine.getJSONFixtures().fixturesPath='base/js/tests/api_mock';
  
          $rootScope = $injector.get('$rootScope');
  
          var $controller = $injector.get('$controller');
          createController = function() {
              return $controller('productsController', {'$scope' : $rootScope });
          };
      }));
  
      afterEach(function() {
          $httpBackend.verifyNoOutstandingExpectation();
          $httpBackend.verifyNoOutstandingRequest();
      });
  
      //Start Tests
      it('Should be array of all products', function() {
          $httpBackend.when('GET', api_root + 'products').respond(
              getJSONFixture('products.json')
          );
  
          var controller = createController();
  
          $httpBackend.flush();
          expect($rootScope.products[3].name).toBe('Ruby on Rails Bag');
      });
  
      it('Should instantiate a new product object from json data', function() {
          $httpBackend.when('GET', api_root + 'products/10').respond(
              getJSONFixture('10.json')
          );
  
          var controller = createController();
  
          $httpBackend.flush();
          expect($rootScope.currentProduct.name).toBe('Spree Ringer T-Shirt');
      });
  }); 
  </code></pre>