$http.timeout in unit test and $httpBackend.flush()

432 views
Skip to first unread message

mko...@koukaam.se

unread,
Sep 6, 2013, 3:49:34 AM9/6/13
to ang...@googlegroups.com
Hi, I am new to angularJS.
I use grunt and karma to develop simple web application and try to write tests whenever possible.
I use angular 1.2.0 rc2 (had the same problem with rc1) and have problem/answer I hope someone can answer me.

I have follwing test case, I have simplified it and reduced it:

it('should abort post request', inject(function($http, $q, $rootScope) {
var canceller = $q.defer();
$http({
method: 'POST',
url: '/xml',
data: '<ping/>',
timeout: canceller.promise,
headers: {
'Content-Type': 'application/xml; charset=utf-8',
'Accept': 'application/xml'
}
});

$rootScope.$digest();
canceller.resolve(); //now the request should be aborted and no request should be pending...

$rootScope.$digest();
}));

What I want to do, is to test that I can abort my request. Based on http://docs.angularjs.org/api/ng.$http  I've used a promise as a timeout.
If I run this test, I receive error "Unflushed requests: 1"

My question is:
Why is do I need to call $httpBackend.flush(); so the test can pass. The request should have been aborted, right? 
Is there something I am missing?

Thank you for guidance
Michal


Reply all
Reply to author
Forward
0 new messages