【问题标题】:Jasmine Controller test: $timeout.flush() causes an 'Unexpected GET request' errorJasmine 控制器测试:$timeout.flush() 导致“意外的 GET 请求”错误
【发布时间】:2014-09-07 15:29:46
【问题描述】:

我有以下测试:

it('should load productGroups into the scope', function(){
  scope._section = 'modifiers';
  scope.userData = {method: 'manual'};
  scope.$digest();
  timeout.flush();//causes the error

  expect(scope.productGroups).toEqual(productGroupService.getProductGroups());
});

现在,我尝试测试的操作发生在超时 0 内,因为我在同步 cookie 中存储的数据时遇到了一些问题。

现在,没有标记线,测试运行 find,除了没有得到预期的结果。 使用标记线,我收到以下错误:

    Error: Unexpected request: GET views/main.html
    No more request expected
        at $httpBackend (/home/oleg/dev/vita-webapp-new/bower_components/angular-mocks/angular-mocks.js:1178:9)
        at sendReq (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:8180:9)
        at $http.serverRequest (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:7921:16)
        at wrappedCallback (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:11319:81)
        at wrappedCallback (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:11319:81)
        at /home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:11405:26
        at Scope.$eval (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:12412:28)
        at Scope.$digest (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:12224:31)
        at Scope.$apply (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:12516:24)
        at Object.fn (/home/oleg/dev/vita-webapp-new/bower_components/angular/angular.js:14023:36)


Process finished with exit code 0

main.html,显然是这个控制器的视图,尝试用下面的代码把它放到templateCache中,但是没有用:

$templateCache.put('views/main.html', $templateCache.get('app/views/views/main.html'));

任何帮助将不胜感激。

【问题讨论】:

  • 这主要是因为当您运行测试模板缓存时什么都没有,所以基本上$templateCache.get('app/views/views/main.html') 必须什么都不给。只需对此设定一个期望并检查...
  • 你能举个例子吗?
  • 我也有这个问题,但是我的flush() 导致$routeProvider.otherwise() 进入我的主要app.js。您是否设法弄清楚它为什么要更改路线?

标签: angularjs unit-testing jasmine karma-runner


【解决方案1】:

我能想到的解决方案是:

$httpBackend.when('GET', 'views/main.html').respond('');

beforeEach 函数中

似乎没有其他方法不起作用。

虽然它是用于单元测试目的的有效解决方案,但它永远无法用于 E2E 测试。

【讨论】:

  • 最后我选择了以下$httpBackend.when('GET', function(name) {name.endsWith('.html');}).respond('');
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-02
  • 2013-05-19
  • 2015-07-08
  • 2023-03-19
  • 2016-01-17
  • 1970-01-01
相关资源
最近更新 更多