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();
});
});
});
Aucun commentaire:
Enregistrer un commentaire