Reputation: 8922
I m not new in test world as I worked for many times in TDD using mocha sinon chai and nodejs.
I m actually a bit disturbed with Angularjs testing and the way it works.
Actually, I need to test the simplest method I can do with this :
/** @test {HateoasService#loadRessource}*/
describe('HateoasService', function() {
let service = null;
let remoteBackend = null;
beforeEach(angular.mock.module('app'));
beforeEach(inject((HateoasService, $httpBackend) => {
remoteBackend = $httpBackend;
service = HateoasService;
}));
afterEach(function() {
remoteBackend.verifyNoOutstandingExpectation();
remoteBackend.verifyNoOutstandingRequest();
});
it('should be resolved a promise with status 200', () => {
remoteBackend.when('GET', 'http://google.fr')
.respond(200, {});
service.loadRessource('http://google.fr').then((res) => {
expect(res.status).toEqual(200);
remoteBackend.flush();
});
});
});
That should cover this code :
loadRessource(ressource) {
return this.http.get(ressource);
}
However, it seems that I m missing something since I m having a trouble with httBackend flushing : Error: Unflushed requests: 1.
Can you help me make this test working ?
Upvotes: 1
Views: 374
Reputation: 574
The function that is the second argument to it
needs a parameter to tell mocha its asynchronous. After flushing, invoke the callback.
it('should be resolved a promise with status 200', done => {
remoteBackend.when('GET', 'http://google.fr')
.respond(200, {});
service.loadRessource('http://google.fr').then((res) => {
expect(res.status).toEqual(200);
remoteBackend.flush();
done();
});
});
Upvotes: 1