Justin
Justin

Reputation: 2334

$httpBackend doesn't seem to be flushing requests

I am testing my Angular app using ngDescribe. I don't think ngDescribe should be too much of a problem here, as it's just managing dependency injection for me. I first began to attempt my test the way the ngDescribe docs say, in the code below I have changed it to a more direct approach just to see if I could get any changes. I am calling a method that in turn calls $http.post('urlname', data); While debugging I can clearly see that my method gets all the way to where it calls post() but then it never continues. Because of this, my test always times out.

Hopefully I've just got something simple that's wrong! Here is my code. The test that fails is "Should work", all the others pass as expected.

Please also note that this is being processed by babel, both the test and the service, before being tested.

Here is the service, it works perfectly when being used. It has a few other variables involved that I have removed, but I assure you those variables are working correctly. While debugging for the tests, I can see that the await is hit, but it never continues past that, or returns. When used in the app, it returns exactly as expected. I THINK this has something to do with ngmock not returning as it should.

async function apiCall (endPoint, data) {
    if (!endPoint) {
        return false;
    }

    try {
        return data ? await $http.post(`${endPoint}`, data) : await $http.get(`${endPoint}`);
    } catch (error) {
        return false;
    }
}

Here are the tests:

ngDescribe({
    name: 'Api Service, apiCall',
    modules: 'api',
    inject: ['apiService', '$httpBackend'],
    tests (deps) {
      let svc;
      beforeEach(() => {
        svc = deps.apiService;
      });
      it('is a function', () => {
        expect(angular.isFunction(svc.apiCall)).toBe(true);
      });
      it('returns a promise', () => {
        const apiCall = svc.apiCall();
        expect(angular.isFunction(apiCall.then)).toBe(true);
      });
      it('requires an endpoint', async () => {
        const apiCall = await svc.apiCall();
        expect(apiCall).toBe(false);
      });
      it('should work', (done) => {
        deps.http.expectPOST('fakeForShouldWork').respond({ success: true });
        const apiCall = svc.apiCall('fakeForShouldWork', {});
        apiCall.then(() => done()).catch(() => done());
        deps.http.flush();
      });
    },
  });

The method being called, apiCall, is simply a promise that is resolved by $http.post().then(); It will also resolve false if an error is thrown.

Since deps.http.expectPOST does not fail, I can tell that the outgoing request is sent. I validated this by changing it to expectGET and then I received an error about it being a POST.

I have tried moving the flush() method to all different parts of the test method, but it seems to make no difference.

Any thoughts? Thanks so much for your help!

Upvotes: 0

Views: 465

Answers (0)

Related Questions