Issue
I am having an issue where I am trying to test a service that has been set up using $resource, and has various methods GET, PUT, POST.
I am using $httpBackend in my tests, and it works fine when testing the GET requests but fails on the PUT/POST - I think it may be due to it sending an OPTIONS request first.
Strangely if I change the factory to use $http.post() instead of using $resource, the test passes fine.
Does anyone know a way around this? Can I turn off the OPTIONS request? Or something...?
Thanks!
service
angular.module('myApp')
.factory('Reports', function ($resource, ApiConfig) {
return $resource(ApiConfig.urlBase + "/protected/HttpResource/:id",{},{
update: {method: 'PUT'},
get: {method: 'GET',isArray: true},
search: {method: 'GET',isArray: false},
save: {method: 'POST'}
});
});
The ApiConfig.urlBase resolves to http://localhost:8080/... in the tests
test file
describe("Reports", function() {
beforeEach(module("myApp"));
beforeEach(inject(function(_Reports_, _$httpBackend_, _ApiConfig_) {
Reports = _Reports_;
$httpBackend = _$httpBackend_;
ApiConfig = _ApiConfig_;
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
describe("save method", function() {
var report = {name: "TestReport", type: "HttpResource"};
beforeEach(function() {
url = ApiConfig.urlBase + "/protected/HttpResource/";
$httpBackend.when("POST", url).respond();
});
it("should make POST request when save method called", function() {
$httpBackend.expectPOST(url);
Reports.save(report);
$httpBackend.flush();
});
});
});
Solution
Okay, so I managed to get this working and it was nothing to do with OPTIONS, just to do with the URL I was checking.
$resource is set up by default to automatically strip trailing slashes - you can turn this off (see the Angular docs for $resource) or I just changed my URL in the test class and removed the last "/".
beforeEach(function() {
url = ApiConfig.urlBase + "/protected/HttpResource";
$httpBackend.when("POST", url).respond();
});
My PUT request used the same URL as above, that confused me for a while as I was expecting the id I was passing through to be used in the URL.
Maybe this helps someone else :)
Answered By - Heather Roberts
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.