$httpBackend.whenGET('/restpath/api/v1/books')
.respond({// some data});
I get the following error
Error: Unexpected request: GET /restpath/api/v1/books
Expected GET /restpath/api/v1/books?limit=10&start=1
For the expectGET I have the following , and this creates dynamic query string. mostly the 'start' parameter, and the whenGET part, I am trying to server a dynamic content depending on the 'start'
$httpBackend.expectGET('/restpath/api/v1/books?limit=10&start=1');
// the actual service goes here , which does the $http service. we don't care
$httpBackend.flush();
EDIT
As of v1.5.0-build.4371 the docs state that the respond callback accepts a
params
argument.So for
'/restpath/api/v1/books?limit=10&start=1'
you will get:PREVIOUS
You use
.expectGET()
if you want to $httpBackend to throw an exception on mismatch..whenGET()
in the other cases.The docs state that
.respond()
can accept either anArray
or a callback function, with the signature:function(method, url, data, headers) {};
Now that we know how to access the request url, in order to serve a dynamic content, we can simply parse the url we receive in the
.respond()
callback using a helper function such as the one posted by Andy E in this question:With this helper in our scope, we can know build a dynamic response, such as:
And voila! You can serve dynamic mock data to your tests.
(for angular apps with versions lower than v1.5.0-build.4371 )
If you dont care about the parameters after your '?' you can do this :
if you care about the first param do this :
if you care about them all do this :
arguments for
whenGET('/restpath/api/v1/')
and
expectGET('restpath/api/v1/books?limit=10&start=1')
are different. They should be same.