TypeError:无法读取未定义的angularjs-grunt测试的属性'then' [英] TypeError: Cannot read property 'then' of undefined angularjs-grunt test

查看:87
本文介绍了TypeError:无法读取未定义的angularjs-grunt测试的属性'then'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用$ q服务进行异步调用.我无法使用业力在单元测试中解决然后"和推迟"的问题.

I'm using $q service to make an async calls. I can't resolve 'then' and 'defer' in unit tests using karma.

以下是我的控制器代码.

The below is my controller code.

scope.filterUrls = [{url:'page1'}, {url: 'page2'}, {url:'page-error'}];

scope.bindFilters = function () {
angular.forEach(scope.filterUrls, function (data) {
    scope.getFilterData(data.url, '').then(function (result) {
      if (data.url === 'page1') {
        scope.moduleData.index = result.data;
      } else if (data.url === 'page2') {
        scope.moduleData.page2 = result.data;
      } 
     });
  });
}

scope.getFilterData = function (filterUrls, params) {
  // $q service object
  var deferred = q.defer();

  // regular ajax request
  http({
    method: 'GET',
    url: app.api.root + filterUrls,
    params: params
  })
      .success(function (result) {
        // promise resolve
        deferred.resolve(result);
      })
      .error(function (result) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
        deferred.reject('Erreur request : ' + result);
      });
  return deferred.promise;
};

测试规范:

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

我收到一个名为"TypeError:无法读取未定义的属性'then'的错误".

I'm getting an error called "TypeError: Cannot read property 'then' of undefined".

如何使用业力为这两种方法编写单元测试.

How can I write an unit test for those two methods using karma.

更新:

1.我们如何测试scope.getFilterData()的成功和错误

1.how do we can test the success and error of scope.getFilterData()

2 .then函数在scope.bindFilters()函数中.

2 .then function in scope.bindFilters() function.

请帮助.

推荐答案

如果仅需要查找是否调用了getFilterData ,请尝试返回通过伪造功能伪造诺言:

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

使用茉莉1.3,我们可以使用andCallFake:

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();
  });

对于茉莉花2.0,我们可以改用and.callFake.

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

另一种解决方案是使用andReturnValue$q.when():

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();
      });

对于茉莉花2.0,我们可以改用and.returnValue.

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

这篇关于TypeError:无法读取未定义的angularjs-grunt测试的属性'then'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆