单元测试角度分页

时间:2016-02-19 16:05:08

标签: angularjs unit-testing pagination jasmine mocha

我在我的标题中使用它来获取所有数据,我在我的控制器中解析(toJson)标题:

X-Pagination:{"current_page":1,"per_page":10,"total_pages":16,"sub_count":10,"total_count":159}

var parsingHeader = angular.fromJson(vms.headers('X-Pagination'));
          _this.current_page = parsingHeader.current_page;
          _this.per_page = parsingHeader.per_page;
          _this.total_pages = parsingHeader.total_pages;
          _this.total_count = parsingHeader.total_count;

我想测试一下:

$httpBackend
      .whenGET('http://localhost:3000/vms.json?page=1')
      .respond(function(method, url, data, headers){
        ...
      });

我有一些错误: '空'不是一个对象(评估' parsingHeader.current_page'),...
测试这个的最佳流程是什么?

1 个答案:

答案 0 :(得分:0)

您需要将标头X-Pagination:{"current_page":1,"per_page":10,"total_pages":16,"sub_count":10,"total_count":159}传递给您的whenGET。我看到没有传递标题,这就是parsingHeader变量为空的原因

做这样的事情:

$httpBackend
      .whenGET('http://localhost:3000/vms.json?page=1', ['X-Pagination':{"current_page":1,"per_page":10,"total_pages":16,"sub_count":10,"total_count":159}] )
      .respond(function(method, url, data, headers){
        ...
      });