TypeError: Cannot read property 'then' of undefined angularjs-grunt test

别说谁变了你拦得住时间么 提交于 2019-12-05 06:55:42

If you only need to find out whether getFilterData is called or not, try returning a fake promise by faking the function:

With jasmine 1.3, we could use andCallFake:

it('should call getFilterData() in bindFilters()', function () {
    spyOn(scope, 'getFilterData').andCallFake(function(){//replace with a fake function
        var deferred = $q.defer(); //assume that you already inject $q service in beforeEach and save it as a variable.
        return deferred.promise; //returns a fake promise
    });
    scope.bindFilters();
    expect(scope.getFilterData).toHaveBeenCalled();
  });

With jasmine 2.0, we could use and.callFake instead.

Another solution is to use andReturnValue and $q.when():

it('should call getFilterData() in bindFilters()', function () {
        spyOn(scope, 'getFilterData').andReturnValue($q.when());
        scope.bindFilters();
        expect(scope.getFilterData).toHaveBeenCalled();
      });

With jasmine 2.0, we could use and.returnValue instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!