unit testing - How to override mocked response of $httpBackend in Angular? -
is possible override or re-define mocked response in mocked $httpbackend?
i have test this:
beforeeach(inject(function ($rootscope, $controller, _$httpbackend_) { $httpbackend = _$httpbackend_; //fake backend $httpbackend.when('get', '/myurl').respond({}); //empty data server ...some more fake url responses... }
this fine cases have few tests need return different same url. seems once when().respond() defined can not change afterwards in code this:
different response in single specific test:
it('should work', inject(function($controller){ $httpbackend.when('get', '/myurl').respond({'some':'very different value long text'}) //create controller //call url //expect {'some':'very different value long text'} returned //but instead response defined in beforeeach }));
how do that? code untestable :(
the docs seem suggest style:
var myget; beforeeach(inject(function ($rootscope, $controller, _$httpbackend_) { $httpbackend = $_httpbackend_; myget = $httpbackend.whenget('/myurl'); myget.respond({}); });
...
it('should work', function() { myget.respond({foo: 'bar'}); $httpbackend.flush(); //now response '/myurl' {foo: 'bar'} });
Comments
Post a Comment