How to actually reset $ httpBackend waiting?

I tried and tried to get this to work. documentation at best:

resetExpectations (); . Resets all query expectations, but retains all backend definitions. Typically, you will call resetExpectations during a multiphase test, when you want to reuse the same instance of $ httpBackend mock.

Each time my second query is called, my result always has the first result data. Check out this script http://jsfiddle.net/tbwn1gt0/2/ , where I reset the wait after the first reset, then set a new wait / result, then rinse again to get the wrong data.

// --- SPECS ------------------------- var url = '/path/to/resource'; var result = ''; describe('$httpBackend', function () { it("expects GET different results in subsequent requests", inject(function ($http, $httpBackend) { successCallback = function(data){ result = data; } // Create expectation $httpBackend.expectGET(url).respond(200, 'mock data'); // Call http service $http.get(url).success(successCallback); // flush response $httpBackend.flush(); console.log( result ); // logs 'mock data' // Verify expectations expect( result ).toContain('mock data'); // works as it should // reset the expectations $httpBackend.resetExpectations(); // set the fake data AGAIN $httpBackend.expectGET(url).respond(200, 'doof the magic cragwagon'); // get the service AGAIN $http.get(url).success(successCallback); expect( result ).toContain('doof'); // does not work, result is original result console.log( result ); // logs 'mock data' })); }); // --- Runner ------------------------- (function () { var jasmineEnv = jasmine.getEnv(); jasmineEnv.updateInterval = 1000; var htmlReporter = new jasmine.HtmlReporter(); jasmineEnv.addReporter(htmlReporter); jasmineEnv.specFilter = function (spec) { return htmlReporter.specFilter(spec); }; var currentWindowOnload = window.onload; window.onload = function () { if (currentWindowOnload) { currentWindowOnload(); } execJasmine(); }; function execJasmine() { jasmineEnv.execute(); } })(); 

Other things I've tried include adding afterEach with the resetExpectations function (putting each request in a new statement). and many other random attempts. If he tries to change the expected URL to something not expected, it will be an error, as he should ... so I know that requests are processed through httpBackend at least.

Is this a defect or am I implementing it incorrectly?

+7
javascript angularjs unit-testing jasmine
source share
3 answers

.resetExpectations() works as you expected, but you just forgot to clear the HTTP request for the second.

 // set the fake data AGAIN $httpBackend.expectGET(url).respond(200, 'doof the magic cragwagon'); // get the service AGAIN $http.get(url).success(successCallback); $httpBackend.flush(); // flush the second http request here expect( result ).toContain('doof'); // does not work, result is original result console.log( result ); // logs 'mock data' 

JSFiddle example: http://jsfiddle.net/4aw0twjf/

PS. In fact, $httpBackend.resetExpectations() not required for your test case.

+4
source

Your case does not require reset expectations. Instead, you can change the wait behavior and create a new one. "expectGET" (and other methods) returns requestHandler, for example:

 // Create expectation var expectationHandler = $httpBackend.expectGET(url).respond(200, 'mock data'); // Call http service, flush response, verify expectations // Modify behavior expectationHandler.respond(200, 'doof the magic cragwagon'); // Call http service ... AGAIN 
+1
source

Since you have multiple calls, I would use this at the end of each test to make sure all HTTP calls succeed expect($httpBackend.flush).not.toThrow(); , which means that you do not need to call $httpBackend.flush(); for every HTTP call.

You are also pleased to add an afterEach block like this.

 afterEach(function () { $httpBackend.verifyNoOutstandingExpectations(); }); 
0
source

All Articles