I have this following factory.
services.factory('Api', ['$resource', function ($resource) {
return $resource(urlPath, {
'action': 'get',
'entity': 'Entity'
}, {
MakePost: {
method: "POST",
isArray: false,
headers: {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'},
transformRequest: function(obj) {
var str = [];
for (var p in obj) {
str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p]));
}
return str.join("&");
}
}
}
);
}]);
Now, i would like to test, when calling Api.MakePost({},{data: {}}, function () {}) the correct header data i.e in this case 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8' is set correctly. Is there away to test this scenario in angularjs, using $httpBackend, Jasmine and spies?
This can be tested in two different ways, with unit and functional tests.
When compared with
$http,$resourcecontains more moving parts, and it makes sense to stub it - at least for some tests.Then provided
$resourcearguments can be tested more thoroughly, e.g.transformRequestmethod can be reached withresourceFactoryStub.calls.first()[2].transformRequestand tested directly.Or the whole thing can be tested in another test with
$httpBackendand real$resource.