【问题标题】:Karma/Jasmine/PhantomJs: undefined is not a constructorKarma/Jasmine/PhantomJs:未定义不是构造函数
【发布时间】:2017-01-10 08:32:51
【问题描述】:

我有一个应用程序在运行测试时引发奇怪的错误。错误如下:

TypeError: undefined is not a constructor (evaluating 'allKeys[i].match(/^[0-9]+$/)') in node_modules/jasmine-core/lib/jasmine-core/jasmine.js (line 2988)
test/spec/core/http/response-spec.js:92:63
loaded@http://localhost:8080/context.js:151:17

这些测试中的大多数都通过了,但很少有中断。这是中断的测试之一:

(function () {
  'use strict';

  describe('MyAccount.core.http.response', function () {
    var ResponseInterceptor = {},
        $httpProvider = {},
        $window = {},
        env = {},
        MessageQueue = {};

    beforeEach(module('MyAccount.core.environment'));
    beforeEach(module('MyAccount.core.http', function (_$httpProvider_, $provide) {
      $httpProvider = _$httpProvider_;

      MessageQueue  = {
        dispatch: jasmine.createSpy('dispatch')
      };

      $window = {
        location: {
          href: jasmine.createSpy()
        }
      };

      $provide.value('$window', $window);
      $provide.value('MessageQueue', MessageQueue);
    }));
    beforeEach(inject(function (_$window_, _ResponseInterceptor_, _env_) {
      $window = _$window_;
      ResponseInterceptor = _ResponseInterceptor_;
      env = _env_;
    }));

    describe('response status', function () {
      // Asserting that 404 and 403 errors are intercepted.
      angular.forEach([404, 403], function (error) {
        describe('is ' + error, function () {
          beforeEach(function () {
            ResponseInterceptor.responseError({
              status: error,
              data: {
                message: 'error ' + error
              }
            });
          });

          it('calls MessageQueue.dispatch with the error message', function () {
            expect(MessageQueue.dispatch).toHaveBeenCalledWith('error ' + error, {
              on: 'global.errors'
            });
          });
        });
      });
    });
  });
})();

我已经坚持了几个小时,似乎无法找到解决方案。以下是我正在使用的依赖项及其版本:

  • 业力:^1.2.0
  • 茉莉花核:^2.5.0
  • 因果报应:^1.0.2
  • karma-phantomjs-launcher:^1.0.2
  • phantomjs:^2.1.7

注意:这是使用角度生成器的全新yeoman 应用程序。

【问题讨论】:

  • 你不是唯一一个,这周我也遇到了同样的问题,我目前正在调试它。就我而言,我正在使用 Maven/frontend-maven-plugin/Jasmine-Core/PhantomJS 到目前为止我发现的情况,就我而言:失败的是相等的数组。是的,相等的数组!如果数组有差异,则测试以常规方式失败( ["a", 12] 不等于 ["b", 56] )。但如果我在 IntelliJ 中手动运行 Karma,一切正常。它没有使用相同的 Jasmine Core(删除 Maven 使用的 Jasmine-Core 并没有阻止手动运行),所以我认为 Jasmine-Core 是罪魁祸首。
  • 您能否确认相等的数组是您的失败的数组?
  • @Darth_Sygnious 我对数组有同样的问题。与其他东西比较时通常会失败。我将尝试降级/升级 JasmineCore 以查看这是否是导致错误的可能原因。

标签: angularjs phantomjs yeoman karma-jasmine


【解决方案1】:

我也遇到了同样的问题,但不久前就解决了。

重复我在 cmets 中所说的:当你有两个 相等 的数组时会发生错误,信不信由你。如果它们不相等,您将得到显示差异的标准误差。

茉莉花核 2.5.0。两天前发布,截至目前。我降级到 2.4.1.,它可以工作。

好像是2.5.0.是罪魁祸首。

降级到 2.4.1.,直到发布者解决它。

我的设置:maven/frontend-maven-plugin/karma(*)/phantomJS

(*) 也可能在这里说“茉莉花”。

【讨论】:

  • 正如我在之前的评论中所说,降级可能是解决方案。你解决了我的问题,非常感谢你:)
  • Jasmine 2.5.1,于 2016 年 9 月 9 日发布,修复了此问题。
猜你喜欢
  • 2017-04-27
  • 2016-08-10
  • 1970-01-01
  • 1970-01-01
  • 2017-08-31
  • 2015-02-10
  • 1970-01-01
  • 2017-08-31
  • 2021-07-15
相关资源
最近更新 更多