javascript - How to actually reset $httpBackend expectations? -
i've tried , tried work. documentation terse, @ best:
resetexpectations(); - resets request expectations, preserves backend definitions. typically, call resetexpectations during multiple-phase test when want reuse same instance of $httpbackend mock.
every time second request called, result has first result's data. check out fiddle http://jsfiddle.net/tbwn1gt0/2/ reset expectations after first flush, set new expectations/result flush again yield incorrect data.
// --- specs ------------------------- var url = '/path/to/resource'; var result = ''; describe('$httpbackend', function () { it("expects 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 should // reset expectations $httpbackend.resetexpectations(); // set fake data again $httpbackend.expectget(url).respond(200, 'doof magic cragwagon'); // service again $http.get(url).success(successcallback); expect( result ).tocontain('doof'); // not work, result 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 resetexpectations (putting each request in new statement). , slew of other random attempts. if try change expected url not expected, errors should-- know requests getting handled through httpbackend @ least.
is defect or implementing incorrectly?
the .resetexpectations() work expected, forgot flush http request second one.
// set fake data again $httpbackend.expectget(url).respond(200, 'doof magic cragwagon'); // service again $http.get(url).success(successcallback); $httpbackend.flush(); // flush second http request here expect( result ).tocontain('doof'); // not work, result original result console.log( result ); // logs 'mock data' example jsfiddle: http://jsfiddle.net/4aw0twjf/
ps. actually, $httpbackend.resetexpectations() not necessary test case.
Comments
Post a Comment