I am testing a controller that uses a service that returns a promise. I need to resolve promise. I am using Jasmine 2.
Here is Spec code
beforeEach(
If you want the spyOn to actually use the correct implementation instead of the mock you can use callThrough()
instead of callFake()
.
Try it like this:
spyOn(myService, "getDateRangeData").and.callThrough();
First of all you are "spying on" the wrong method.
We use spyOn
for two reasons:
expect(method).toHaveBeenCalled
return value
In your case the spyOn
does not achieve any of these two.
You should spyOn
the $http
instead. Since the actual http call is not required for your test, the reason being: the objective is not to test $http
.
this.$http = $http;
spyOn(this, '$http').and.callFake(function(args) {
return {
then: function(fn) {
return fn('response');
}
};
});
And in it
block:
it('getDateRangeData return Data obj', function() {
myService.getDateRangeData('test')
.then(function(response) {
console.log('Success', response);
expect(response).toEqual('response');
});
expect(this.$http).toHaveBeenCalledOnceWith('test');
});