I have a service that calls a REST URL that returns data.
My sample code would be
$http('POST', '/mockUrl/resource/'+resourceValue).then(...);
My service works fine and it returns data. My problem is how do I test this in karma. Right now I have a different resourceValue to be tested for the mockUrl being called. Before coming to stackoverflow, in each test i was defining $httpBackend with the expected URL.
eg:
it('testing for status 200', function(){
$httpBackend.when('POST', '/mockUrl/resource/'+1234)
.respond(function (method, url, data, headers) {
return [200, data1];
});
MyService.serviceMethod(1234).then(function(){
//test the returned data
});
});
it('testing for status 201', function(){
$httpBackend.when('POST', '/mockUrl/resource/'+4567)
.respond(function (method, url, data, headers) {
return [201, data2];
});
MyService.serviceMethod(1234).then(function(){
//test the returned data
});
});
I am being told that I should not be writing my karma tests in the above manner. But I am not sure how to avoid that.
I have tried
$httpBackend.when('POST',url)
.respond(function (method, url, data, headers) {
return data[option];
});
But this 'url' never gets called in any test. I am not sure how to proceed further.