Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit-testing a controller that uses $http
    primarykey
    data
    text
    <p>I have a simple controller and the first thing I need it to do is assign a value to scope.</p> <pre><code>function TestCtrl($scope, $http) { $scope.listForms = 'some list'; } </code></pre> <p>The following test for the controller works as expected:</p> <pre><code>describe('Testing a controller', function() { var ctrl, scope, httpMock; beforeEach(inject(function($injector) { scope = $injector.get('$rootScope').$new(); ctrl = $injector.get('$controller'); ctrl(TestCtrl, { $scope: scope }); })); it("assigns to scope", function() { expect(scope.listForms).toMatch("some list"); }); }); </code></pre> <p>But when I change the function to get the list from my API</p> <pre><code>function TestCtrl($scope, $http) { $http.get('/api/listForms').success(function(list) { $scope.aListOfForms = 'some list'; }); } </code></pre> <p>and the test changes to</p> <pre><code>describe('Testing a controller', function() { var ctrl, scope, httpMock; beforeEach(inject(function($injector) { httpMock = $injector.get('$httpBackend'); scope = $injector.get('$rootScope').$new(); httpMock.when('GET', '/tactical/api/listOrderForms').respond("an order form"); ctrl = $injector.get('$controller'); ctrl(TestCtrl, { $scope: scope, $http: httpMock }); })); it("gets the list from the api and assigns it to scope", function() { httpMock.expectGET('tactical/api/listOrderForms'); expect(scope.orderFormList).toMatch("an order form"); httpMock.flush(); }); }); </code></pre> <p>I get the following errors:</p> <pre><code>TypeError: 'undefined' is not a function Expected undefined to match 'an order form'. Error: No pending request to flush ! </code></pre> <p>Does anyone know what I am doing wrong? Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload