【问题标题】:Jasmine httpBackend-related tests broken when upgrading Angular 2.21 to 2.25将 Angular 2.21 升级到 2.25 时,Jasmine httpBackend 相关测试中断
【发布时间】:2014-09-17 08:08:03
【问题描述】:

我在 Jasmine 和 Karma 中有许多依赖于 $httpBackend HTTP 模拟检查的测试。当我今天升级 Angular 时,他们停止工作了。

假设我的 Jasmine 测试中有类似的内容:

    // Arrange
    httpBackend.expect('POST', 'https://localhost:44300/api/projects/1/samples').respond(fakedDto);
    // Act
    scope.updateSamples();
    httpBackend.flush();

在最后一行,之前完全没问题,我收到:

    TypeError: $browser.$$checkUrlChange is not a function in C:/SVN/samplemgmt/src/ClientApp/client/bower_components/angular/angular.js (line 12502)
$RootScopeProvider/this.$get</Scope.prototype.$digest@C:/SVN/samplemgmt/src/ClientApp/client/bower_components/angular/angular.js:12502:9
createHttpBackendMock/$httpBackend.flush@C:/SVN/samplemgmt/src/ClientApp/client/bower_components/angular-mocks/angular-mocks.js:1481:5
@C:/SVN/samplemgmt/src/ClientApp/tests/integration/sample/samplecreationController_integration.test.js:341:9

此外,我有通常的配置来结束测试:

afterEach(function () {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
});

现在开始产生以下错误:

Error: [$rootScope:inprog] $digest already in progress
http://errors.angularjs.org/1.2.25/$rootScope/inprog?p0=%24digest in C:/SVN/samplemgmt/src/ClientApp/client/bower_components/angular/angular.js (line 78)
minErr/<@C:/SVN/samplemgmt/src/ClientApp/client/bower_components/angular/angular.js:78:5
beginPhase@C:/SVN/samplemgmt/src/ClientApp/client/bower_components/angular/angular.js:13009:9
$RootScopeProvider/this.$get</Scope.prototype.$digest@C:/SVN/samplemgmt/src/ClientApp/client/bower_components/angular/angular.js:12500:9
createHttpBackendMock/$httpBackend.verifyNoOutstandingExpectation@C:/SVN/samplemgmt/src/ClientApp/client/bower_components/angular-mocks/angular-mocks.js:1514:5
@C:/SVN/samplemgmt/src/ClientApp/tests/integration/sample/samplecreationController_integration.test.js:137:9

我很欣赏一些关于如何让事情再次发挥作用的建议。最近对 httpBackend 测试有什么变化吗?

【问题讨论】:

  • 我对 httpBackend.flush() 和 $scope.$apply() 也有类似的问题。我没有得到“$digest 已经在进行中”,但是当我在 1.2.25 的 jasmine 测试中调用上述任何一个角度方法时,我确实得到了“TypeError:undefined is not a function”,但在 Angular 1.2.22 中没有。
  • 你能发布更多关于你的测试的信息吗?如果您在错误的位置调用 $scope.$apply,则会发生错误“$digest already in progress”。首先,您的测试似乎是可以的,可能是您测试的代码中的错误?
  • 我认为的问题是angular-mocks的版本必须和angular一样,所以你也升级了angular-mocks?

标签: angularjs jasmine karma-jasmine


【解决方案1】:

对于那些使用旧 Angular 并且由于某种原因目前无法升级的用户,here 是一篇很好的帖子,它解释了发生了什么以及如何解决问题。

总结一下——

出现此问题是因为 $httpBackend.flush() 和 $httpBackend.verifyNoOutstandingExpectation() 都错误地尝试进行摘要。显然,内部的(来自verifyNoOutstandingExpectation() 调用)失败了。幸运的是,我们可以指示它不要执行摘要 - 请注意 false 参数:

afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation(false); // <-- here
    $httpBackend.verifyNoOutstandingRequest();
});

【讨论】:

    【解决方案2】:

    已解决:此时必须手动更新 Angular-Mocks(不使用 bower..)。

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2019-12-02
      相关资源
      最近更新 更多