Test case for $http request using jasmine - angularjs

Currently I am writing test cases in jasmine for my stand-alone angularJs app, I need to write test cases for $http requests also, I wrote the tests to match the response from $http request and $httpBackend request, but that is not enough, what else can I verify for a $http request other than response status.
Can format of data returned be verified? and what other stuff can be checked in a test spec?
Thanks!!

Have a look at Spies: http://angular-tips.com/blog/2014/03/introduction-to-unit-test-spies/
Spy on a service method call using jasmine Spies
You can do anything with a request. You can fake all responses (success, error), fire backend requests, which are faked to local json-files and so on.
Read more about jasmine / karma testing | jasmine / karma fake backend.

Related

Differences between Angular's $httpBackend expectPost and whenPost

While working on some tests, I was surprised to find, that simply changing the $httpBackend.expectPost to a $httpBackend.whenPost fixed some broken tests...
Looking at the docs, it says that an expect "Creates a new request expectation.", while a when "Creates a new backend definition.". Unfortunately this doesn't mean much too me...
Can someone explain the difference between these two methods?
As mentioned in the docs,
Request expectations provide a way to make assertions about requests
made by the application and to define responses for those requests.
The test will fail if the expected requests are not made or they are
made in the wrong order.
Backend definitions allow you to define a fake backend for your
application which doesn't assert if a particular request was made or
not, it just returns a trained response if a request is made. The test
will pass whether or not the request gets made during testing.
With whenPost() definition, whenever your code makes a POST request through $http, this method will intercept and serve the response. But in case of expectPost(), it actually creates an expectation about POST request to that URL and if your code doesn't make any POST request to that URL, test will fail.
In case a request is made, then it will also respond with mock object.

Http interceptor response cache vs server

In my angular app I have an http interceptor that will perform some operation fn(x) on every response from the server. The problem I have encountered is that the angular $http service will still hit the interceptor when it is returning a response from its own cache. So, because one of my $http calls is using cache = true in it's config, I end up executing fn(x) on calls that do not reach the server.
Is there any lower level way in angular to determine whether the response actually came from the server rather than from angular's cache?

Karma Jasmine AngularJS testing with real HTTP requests

I want to test my code with real API calls (so I can test the API as well, and when I change the API I don't have to change the JS test as well, and a lot more benefits.) instead of the regular $httpBackend.expectPOST('http://api.com/login').response(200).
Essentially, I want to test a ProductsController that expects to be logged in through an AuthService.login() method and receive a list of products through ui-router's resolve feature.
In this case, the login method receives data that needs to be used to gather products.
From the $httpBackend documentation found here: https://docs.angularjs.org/api/ngMockE2E/service/$httpBackend
As opposed to unit-testing, in an end-to-end testing scenario or in scenario when an application is being developed with the real backend api replaced with a mock, it is often desirable for certain category of requests to bypass the mock and issue a real http request (e.g. to fetch templates or static files from the webserver). To configure the backend with this behavior use the passThrough request handler of when instead of respond
So, something like: $httpBackend.whenGET(/.*/).passThrough(); should suffice.

Scala Test : Mock deadbolt authentication

While running scala test, how can deadbolt auth be mocked so that the requests goes through?

Setting the JSONP callback function in AngularJS

I'm trying to fetch data from a web API via Angular $resource service. The service exposes JSONP interface, but does not allow setting the callback name. Everything works well, my requests goes out, the data returns, the script is injected and then it fails because the callback function is not defined.
Angular documentation is very sparse on this, but it seems that the default callback function Angular sets up is: JSON_CALLBACK, and there's no info how to change that so that it matches the function returned by the foreign API.
Thanks.
I don't think that there is any provision to override that callback.
$resource is high level Restful api based on $http service.
You can use $http apis which returns http promise object and letting you write your success callback wherein you can process data returned from ajax request.
e.g. http://docs.angularjs.org/api/ng.$http#jsonp

Resources