Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine : Expect $http.post() not have been called

In an angularjs program I'd like to test with Jasmine if an http post is NOT performed in a test.

I've try the following code :

expect($http.post).not().toHaveBeenCalled();

But I get "ReferenceError: $http is not defined"

like image 503
W Lambert Avatar asked Jan 24 '14 16:01

W Lambert


Video Answer


2 Answers

This is old but I used the following to test for this.

expect($httpBackend.flush).toThrowError('No pending request to flush !');
like image 125
efarley Avatar answered Oct 27 '22 10:10

efarley


That is error is because you never injected $http into the test. You can do this with the inject function, but for testing $http calls, you really should use $httpBackend

For requests that you want to make sure they aren't called, you don't need to do anything. Angular throws an error when it gets a request that wasn't expected (as defined by the expect functions on $httpBackend). So If a request is made that shouldn't be, the tests will fail from this error thrown from an unexpected request.

like image 22
dnc253 Avatar answered Oct 27 '22 08:10

dnc253