how to test and resolve Controller data (.then function()) promise and get orginal Data in Jasmine2

泪湿孤枕 提交于 2019-11-28 02:25:23

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:

  • To expect(method).toHaveBeenCalled
  • To mock the 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');   
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!