After reading so many posts on AngularJS testing with Jasmine, I came to the conclusion that the way I built my service is not a testable one.
But still, I'm too much of a noob on AngularJS to really grasp a better way of doing it. Some of my colleagues tell me that I can test nested methods, but it doesn't seem so.
So, here's the situation:
I have a service that performs a GET to a restfull webservice and returns an object.
it looks like so:
.factory('MyService', function( $resource, config, $q ) {
return config.get("RestServiceBaseUrl").then(function(baseUrl) {
return $resource(baseUrl+"params_necessary_to_my_rest_call")
})
})
It has that config injected, which is a service as well, that will return me whatever I have in a physical config file, in this case, it's a url, that is stored under a RestServiceBaseUrl JSon variable. Anyhow, here's the get part of my injected config object:
function get(sKey) {
var deferred = $q.defer()
ConfigGet(sKey, function (resp) {
deferred.resolve(resp['result'])
})
return deferred.promise
}
So, as you can see, I have to nested returns, one for the config.get method, whence comes from a service; and the other one is the return from the GET, which is also dependent on the $resource, injected as well on my service. That $resource comes from the angular-resources. Nothing has been done to override it.
In my unit tests, I have tried so many ways of achieving a test, but it's frustrating, because when I get passed the config problem, I get the $resource problem, and I can't seem to do it right when it comes down to testing services with injected services.
Here's my code at Plunker: http://plnkr.co/edit/RSFTKq
As you can see, I'm attempting to test the returning value form my service.
I'm on this thing for 4 days already! I'm in despair! Any help will be greatly appreciated!
Please, excuse my English mistakes.
Regards