1

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 ?

1 Answer 1

1

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();
});

});

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.