lundi 27 juillet 2015

Fixing unexpected GET request in service unit test

In my unit test for a service, I'm testing a GET request. I'm getting the following error, even when I explicitly state $httpBackend.expectGET('/api/example').respond(200, {});.

Error: Unexpected request: GET /api/another/example
Expected GET /api/example

Unit test

 describe('MyService', function() {
    var MyService,
        $httpBackend;

    beforeEach(module('example'));

    beforeEach(function() {
      inject(function(_MyService_, _$httpBackend_) {
        MyService = _MyService_;
        $httpBackend = _$httpBackend_;

        $httpBackend.expectGET('/api/example').respond(200, {});
      });
    })

    describe('#get', function() {
      beforeEach(function() {

        // this is what i want to test
        $httpBackend
          .expectGET('/api/another/example')
          .respond(200, {});

      });

      it('should send a get request', function() {
        MyService.get();

        $httpBackend.flush();
      });
    });

  });

Aucun commentaire:

Enregistrer un commentaire