title: $httpBackend in AngularJs Jasmine unit test
body: <p>I'm unable to get my unit test to work properly. I have a $scope array that starts out empty, but should be filled with an $http.get(). In the real environment, there'd be approx 15 or so objects in the array, but for my unit test I just grabbed 2. For the unit test, I have:</p>
  
  <pre><code>expect($scope.stuff.length).toBe(2);
  </code></pre>
  
  <p>But jasmine's error is: Expected 0 to be 2.</p>
  
  <p>here's my controller.js:</p>
  
  <pre><code>$scope.stuff = [];
  $scope.getStuff = function () {
      var url = site.root + 'api/stuff';
      $http.get(url)
          .success(function (data) {
              $scope.stuff = data;
          })
          .error(function(error) {
              console.log(error);
          });
  };
  </code></pre>
  
  <p>and my controller.spec.js is:</p>
  
  <pre><code>/// &lt;reference path="../../../scripts/angular-loader.min.js" /&gt;
  /// &lt;reference path="../../../scripts/angular.min.js" /&gt;
  /// &lt;reference path="../../../scripts/angular-mocks.js" /&gt;
  /// &lt;reference path="../../../scripts/angular-resource.js" /&gt;
  /// &lt;reference path="../../../scripts/controller.js" /&gt;
  beforeEach(module('ui.router'));
  beforeEach(module('ngResource'));
  beforeEach(module('ngMockE2E'));
  beforeEach(module('myApplication'));
  var $scope;
  var controller;
  var httpLocalBackend;
  
  beforeEach(inject(function ($rootScope, $controller, $injector) {
      $scope = $rootScope.$new();
      controller = $controller("StuffController", {
          $scope: $scope
      });
  }));
  
  beforeEach(inject(function ($httpBackend) {
      httpLocalBackend = $httpBackend;
  }));
  
  it('should get stuff', function () {
      var url = '/api/stuff';
      var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
      httpLocalBackend.expectGET(url).respond(200, httpResponse);
      $scope.getStuff();
      expect($scope.stuff.length).toBe(2);
      //httpLocalBackend.flush();
  } );
  </code></pre>
  
  <p>Now, obviously, I changed variable names and such since this is for work, but hopefully this is enough information for anybody to help me. I can provide more if needed. I also get a second error when uncommenting the .flush() line, but I'll get to that a bit later.</p>
  
  <p>Any help is greatly appreciated and thanks in advance!</p>
  
  <p>EDIT:</p>
  
  <p>Finally got it working! Here's my final code:</p>
  
  <pre><code>it('should get stuff', function () {
      var url = '/api/stuff';
      var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
      httpLocalBackend.expectGET(url).respond(200, httpResponse);
      $scope.getStuff();
      httpLocalBackend.flush();
      expect($scope.stuff.length).toBe(2);
  } );
  </code></pre>
  
  <p>EDIT 2:
  I ran into another problem, which I think may have been the root cause of this. See <a href="https://stackoverflow.com/questions/25794501/unit-test-failing-when-function-called-on-startup">Unit test failing when function called on startup</a></p>