Dan Tao
Dan Tao

Reputation: 128357

How do I expect an HTTP request NOT to be made?

Angular's $httpBackend service lets you expect an HTTP request with expectGET, expectPOST, etc. (or just expect).

How would I write a test that says, "the controller should NOT make a request to this endpoint (under these conditions)"?

I was thinking something like:

$httpBackend.when('/forbidden/endpoint').respond(function() {
  throw Error("Shouldn't be making a request to /forbidden/endpoint!");
});

That seems a bit hacky to me, but I'm fine with it if that's the normal way to do things. (But I doubt that.)

Upvotes: 9

Views: 5195

Answers (4)

Ilya Luzyanin
Ilya Luzyanin

Reputation: 8110

One solution might be to check if $httpBackend.flush() throws an exception, since there should be nothing to flush:

beforeEach(function() {
   $httpBackend.whenGET('/forbidden/endpoint');
   ...
   // call service method under test (that should not make $http call)
});

it('Should not call the endpoint', function() {
    expect($httpBackend.flush).toThrow();
});

Important thing to note: we use when and not expect, since we don't actually expect the call to be made. And since there is no call, $httpBackend.flush() will throw an exception: No pending request to flush.

Upvotes: 2

user5559873
user5559873

Reputation: 1

$httpBackend is not applied because the $http call doesn't get made in this test.

Instead, you can inject $http in your test, and then spyOn() $http directly:

beforeEach(fn () { 
  inject(function ($injector) {
    this.service = $injector.get('serviceOrControllerOrDirectiveBeingTested');
    this.$http = $injector.get('$http');
  }
});

and then

it('should ...', fn() {
  spyOn(this.$http, 'get');
  this.service.methodThatTriggersTheHttpCall();
  expect(this.$http.get).not.toHaveBeenCalled();
});

Upvotes: 0

lumio
lumio

Reputation: 7585

I stumbled over the same issue.

The solution would be to have a callback function as response and inside you could expect(true).toBe(false) or in my opinion something a little bit more beautiful:

it ('should not trigger HTTP request', function() {
    var forbiddenCallTriggered = false;
    $httpBackend
      .when('/forbidden/endpoint')
      .respond(function() {
        forbiddenCallTriggered = true;
        return [400, ''];
      });

    // do whatever you need to call.

    $rootScope.$digest();
    $httpBackend.flush();

    // Let test fail when request was triggered.
    expect(forbiddenCallTriggered).toBe(false);
  });

Upvotes: 12

Sunil D.
Sunil D.

Reputation: 18193

For scenarios like this I often use Jasmine's spyOn() function. You can spy on functions of $http, $resource, or of a custom service (like myServiceThatUsesHTTP below):

spyOn(myServiceThatUsesHTTP, 'query');
// test, then verify:
expect(myServiceThatUsesHTTP.query).not.toHaveBeenCalled();
// or
expect(myServiceThatUsesHTTP.query.callCount).toBe(0);

When you spyOn() a function, the original function is replaced. The code for the original function is not executed, which can be good or bad (depending on what you need to do for the test).

For example, if you need the $promise object that $http or $resource returns, you can do this:

spyOn($http, '$get').andCallThrough(); 

Upvotes: 5

Related Questions