Note that there are some explanatory texts on larger screens.

plurals
  1. POangular.js how to mock(or stub) xhr request in each test
    primarykey
    data
    text
    <p>In angular js unit test, I wanted to set xhr response to each test(in "it" method"), not in beforeEach method, but it seems not working.?</p> <p>This works</p> <pre><code>describe('ExampleListCtrl', function(){ var $scope, $httpBackend; beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) { $httpBackend = _$httpBackend_; $httpBackend.expectGET('examples').respond([]); // &lt;--- THIS ONE $controller(ExampleListCtrl, {$scope: $scope = $rootScope.$new()}); })); it('should create "examples" with 0 example fetched', function() { expect($scope.examples).toBeUndefined(); $httpBackend.flush(); expect($scope.examples).toEqual([]); }); }); </code></pre> <p>result</p> <pre><code>Executed 8 of 8 SUCCESS (0.366 secs / 0.043 secs) </code></pre> <p>But this fails with error when I move expectGet method to each method. I don't know why.</p> <pre><code>describe('ExampleListCtrl', function(){ var $scope, $httpBackend; beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) { $httpBackend = _$httpBackend_; $controller(ExampleListCtrl, {$scope: $scope = $rootScope.$new()}); })); it('should create "examples" with 0 example fetched', function() { $httpBackend.expectGET('examples').respond([]); // &lt;--- MOVED TO HERE expect($scope.examples).toBeUndefined(); $httpBackend.flush(); expect($scope.examples).toEqual([]); }); }); </code></pre> <p>Here is the error</p> <pre><code>.... at /Users/me/app/test/unit/controllers/ExampleListCtrlSpec.js:3:1 Error: No pending request to flush at Error (&lt;anonymous&gt;) at Function.$httpBackend.flush (/Users/me/app/test/lib/angular/angular-mocks.js:1171:34) at null.&lt;anonymous&gt; (/Users/me/app/test/unit/controllers/ExampleListCtrlSpec.js:14:18) </code></pre> <p>---- Edited ----</p> <p>Following the advice below, I have moved the controller out of beforeEach, and changed test like this, so that I can test $httpBackend.expectGET more than once.</p> <pre><code>describe('ExampleListCtrl', function(){ var $scope, $rootScope, $httpBackend; beforeEach(inject(function(_$httpBackend_, $rootScope, $controller) { $httpBackend = _$httpBackend_; controller = $controller; $scope = $rootScope.$new(); })); it('should create "examples" model with 0 example fetched from xhr', function() { $httpBackend.expectGET('examples').respond([]); controller(ExampleListCtrl, {$scope: $scope}); $httpBackend.flush(); expect($scope.examples).toEqual([]); }); }); </code></pre>
    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.
 

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