【问题标题】:Unit Test Angular Pagination单元测试角度分页
【发布时间】:2016-06-01 06:44:20
【问题描述】:

我在我的标头中使用它来获取所有数据,我在我的控制器中解析 (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){
        ...
      });

我有一些错误: “null”不是对象(正在评估“parsingHeader.current_page”),...
测试这个的最佳过程是什么?

【问题讨论】:

    标签: angularjs unit-testing pagination jasmine mocha.js


    【解决方案1】:

    您需要将标头 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){
            ...
          });
    

    【讨论】:

    • 你能给我举个例子吗?在我的回复或 URL 中?
    • 阅读 Angular 文档:docs.angularjs.org/api/ngMockE2E/service/$httpBackendwhenGet 支持标题...请参阅 whenGET(url, [headers], [keys]);
    • $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){console.log(data);}); 它返回:没有更多的请求预期
    • 我没有检查头参数的语法。请做更多的研究,可能有错字。如果您的请求不正确(可能是拼写错误),或者您没有列出您要提出的所有请求,则不会再发生请求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 2015-04-25
    相关资源
    最近更新 更多