【问题标题】:unexpected GET request on karma unit testing对业力单元测试的意外 GET 请求
【发布时间】:2016-12-25 23:40:22
【问题描述】:

我正在使用 karma 测试我的 Angular 应用程序中的控制器,它自己的应用程序按预期工作,但单元测试抛出 error: unexpected request: GET views/home.html 当我遇到问题时,问题出在 ui-router 和 $httpBackend 上。所以一种方法是通过“业力预处理器”进行缓存,但我所做的是:

var MenuController, scope, $httpBackend;
// Initialize the controller and a mock scope
beforeEach(inject(function($controller, _$httpBackend_, $rootScope, menuService) {
// place here mocked dependencies
$httpBackend = _$httpBackend_;
$httpBackend.whenGET("views/header.html").respond(200,'');
$httpBackend.expectGET("http://localhost:3000/dishes").respond([{ ....

但我的视图文件夹中的每个模板都会显示 GET 错误。

现在有什么方法可以忽略所有模板和任何其他意外的 GET 请求?

【问题讨论】:

    标签: phantomjs karma-jasmine httpbackend angular-mock


    【解决方案1】:

    您可以创建一个匹配所有模板请求的正则表达式: httpBackend.whenGET(/views\/.*\.html/).respond(200, {}); 或仅 httpBackend.whenGET(/views\//).respond(200, {})

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 2014-11-27
      • 2021-04-25
      • 1970-01-01
      • 2016-06-19
      相关资源
      最近更新 更多