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"
This is old but I used the following to test for this.
expect($httpBackend.flush).toThrowError('No pending request to flush !');
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With