【问题标题】:Karma AngularJS bootstrap and window variableKarma AngularJS 引导程序和窗口变量
【发布时间】:2016-10-13 15:42:57
【问题描述】:

我正在尝试为我的应用程序编写一个测试,并且我正在使用 Karma 将文件加载到 PhantomJS 中。问题是其中一个文件由于窗口变量而导致页面重新加载。

文件是这样包含的:

files: [
    'bower_components/angular/angular.js',
    'js/**/*.js'
]

主应用程序文件 (main.js) 包含以下内容:

if ( window.top !=== window.self ){
    window.location.href = 'someOtherURL.html';
}

// PhantomJS 2.1.1 ERROR
// Some of your tests did a full page reload!

所以我的测试都没有运行,因为包含这个文件会因页面重新加载而停止执行。

我的问题是,如何设置窗口变量以便运行此测试?

【问题讨论】:

    标签: javascript angularjs phantomjs karma-runner


    【解决方案1】:

    由于测试环境中没有全局窗口对象,您可以在测试前放置以下 hack 字符串:

    window = window || {};
    

    【讨论】:

    • 这会在 karma 配置中使用吗?
    • 例如:你可以把它放在单独的文件中,然后先包含在测试文件中
    【解决方案2】:

    window.top 和 window.self 不一样的原因是 karma 默认在 iframe 中加载测试。

    将此添加到我的业力配置中:

    client: {
        useIframe: false
    },
    

    现在它可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 2018-06-21
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      相关资源
      最近更新 更多