unit testing - How to avoid the 'Error: Unexpected request: GET' every time an AngularJS test does a rootScope.digest() or httpBackend.flush() -
all unit tests, not e2e tests, explicit rootscope.digest() or httpbackend.flush() flush asynchronous callback, experience error:
how avoid 'error: unexpected request: get' no more request expected
i reckon because httpbackend calls ui-router template. don't know why wants so. i'm not asking this. want call mocked json service.
this error forces me have following statement in each it() block:
$httpbackend.whenget(/\.html$/).respond('');
there must neater way.
specially if test has no use of $httpbackend in first place:
it('should return list of searched users', function() { // use statement avoid error $http service making request application main page $httpbackend.whenget(/\.html$/).respond(''); var users = null; userservice.search('toto', 1, 10, 'asc', function(data) { users = data.content; }); $rootscope.$digest(); expect(users).toequal(restservice.allusers.content); });
the test passes looks hackish. or noobish :-)
edit: test:
it('should return list of users', function () { // use statement avoid error $http service making request application main page $httpbackend.whenget(/\.html$/).respond(''); // create mock request , response $httpbackend.expectget(env.nitro_project_rest_url + '/users/1').respond(mockeduser); // exercise service var user = null; restservice.user.get({userid: 1}).$promise.then(function(data) { user = data; }); // synchronise $httpbackend.flush(); // check callback data expect(user.firstname).toequal(mockeduser.firstname); expect(user.lastname).toequal(mockeduser.lastname); });
this design, tests should checking http calls being made , they're requesting correct url. instead of checking whether requests made /\.html$/
why not instead check whether requests made correct endpoints? whether directives partial or api call.
if insist on throwing away useful test, move whenget()
beforeeach()
.
Comments
Post a Comment