【问题标题】:getting angular and intern.io working together让 Angular 和 intern.io 一起工作
【发布时间】:2014-09-26 17:07:58
【问题描述】:

我正在尝试设置 Intern.io,以便测试我的 Angular 应用程序。我也在使用 Grunt 和 Bower。我只是想获得自动运行测试的准系统监视任务。当我尝试运行测试时,我收到以下错误消息:

Running "intern:app" (intern) task
ReferenceError: window is not defined
    at /Users/evanvandegriff/Documents/work/nomi_v2/nomi_v2/web/vendor/src/angular/angular.js:19288:3
    at Function.vm.runInThisContext (/Users/evanvandegriff/Documents/work/nomi_v2/nomi_v2/web/node_modules/intern/node_modules/istanbul/lib/hook.js:163:16)
    at /Users/evanvandegriff/Documents/work/nomi_v2/nomi_v2/web/node_modules/intern/node_modules/dojo/dojo.js:760:8
    at fs.js:271:14
    at Object.oncomplete (fs.js:107:15)
Warning: ReferenceError: window is not defined
    at /Users/evanvandegriff/Documents/work/nomi_v2/nomi_v2/web/vendor/src/angular/angular.js:19288:3
    at Function.vm.runInThisContext (/Users/evanvandegriff/Documents/work/nomi_v2/nomi_v2/web/node_modules/intern/node_modules/istanbul/lib/hook.js:163:16)
    at /Users/evanvandegriff/Documents/work/nomi_v2/nomi_v2/web/node_modules/intern/node_modules/dojo/dojo.js:760:8
    at fs.js:271:14
    at Object.oncomplete (fs.js:107:15) Use --force to continue.

我的 app.js 看起来像:

angular.module( 'ngNomi', [

])

.config( function myAppConfig () {

})

.run( function run () {

})

.controller( 'AppCtrl', function AppCtrl ( $scope, $location ) {

});

这是我的 intern.js 文件:

// Learn more about configuring this file at <https://github.com/theintern/intern/wiki/Configuring-Intern>.
// These default settings work OK for most people. The options that *must* be changed below are the
// packages, suites, excludeInstrumentation, and (if you want functional tests) functionalSuites.
define({
    // The port on which the instrumenting proxy will listen
    proxyPort: 9000,

    // A fully qualified URL to the Intern proxy
    proxyUrl: 'http://localhost:9000/',

    // Default desired capabilities for all environments. Individual capabilities can be overridden by any of the
    // specified browser environments in the `environments` array below as well. See
    // https://code.google.com/p/selenium/wiki/DesiredCapabilities for standard Selenium capabilities and
    // https://saucelabs.com/docs/additional-config#desired-capabilities for Sauce Labs capabilities.
    // Note that the `build` capability will be filled in with the current commit ID from the Travis CI environment
    // automatically
    capabilities: {
        'selenium-version': '2.41.0'
    },

    // Browsers to run integration testing against. Note that version numbers must be strings if used with Sauce
    // OnDemand. Options that will be permutated are browserName, version, platform, and platformVersion; any other
    // capabilities options specified for an environment will be copied as-is
    environments: [
        { browserName: 'internet explorer', version: '11', platform: 'Windows 8.1' },
        { browserName: 'internet explorer', version: '10', platform: 'Windows 8' },
        { browserName: 'internet explorer', version: '9', platform: 'Windows 7' },
        { browserName: 'firefox', version: '28', platform: [ 'OS X 10.9', 'Windows 7', 'Linux' ] },
        { browserName: 'chrome', version: '34', platform: [ 'OS X 10.9', 'Windows 7', 'Linux' ] },
        { browserName: 'safari', version: '6', platform: 'OS X 10.8' },
        { browserName: 'safari', version: '7', platform: 'OS X 10.9' }
    ],

    // Maximum number of simultaneous integration tests that should be executed on the remote WebDriver service
    maxConcurrency: 3,

    // Name of the tunnel class to use for WebDriver tests
    tunnel: 'SauceLabsTunnel',

    // The desired AMD loader to use when running unit tests (client.html/client.js). Omit to use the default Dojo
    // loader
    // useLoader: {
    //  'host-node': 'dojo/dojo',
    //  'host-browser': 'node_modules/dojo/dojo.js'
    // },

    // Configuration options for the module loader; any AMD configuration options supported by the specified AMD loader
    // can be used here
    loader: {
        packages: [
            { name: 'angular', location: 'vendor/src/angular/angular' },
            { name: 'angular-mocks', location: 'vendor/src/angular/angular-mocks' }
        ]
    },

    // Non-functional test suite(s) to run in each browser
    suites: [ 'src/app.test.js' ],

    // Functional test suite(s) to run in each browser once non-functional tests are completed
    functionalSuites: [ /* 'myPackage/tests/functional' */ ],

    // A regular expression matching URLs to files that should not be included in code coverage analysis
    excludeInstrumentation: /^node_modules/
});

基本上,我如何在运行测试时在该文件中定义窗口?

【问题讨论】:

  • 第 24699 行显然是:})(window, document);所以它期待一个窗口上下文发送到 iffy,并且 node.js 上下文中没有窗口。
  • 我认为你的意思是在磁贴@Evan 中的“intern.js”

标签: javascript angularjs gruntjs bower intern


【解决方案1】:

我认为这是对 intern.js 如何看待测试环境的期望问题。 Angular 天生就在浏览器上下文中运行,因此它需要一个全局窗口。第 24699 行的主要 iffe 确实(窗口)。我们必须使用 intern-runner 而不是 intern-client,这样我们才能驱动具有窗口上下文的浏览器。

但您可能希望像 Karma 示例所示的那样快速无头运行。因此,我们只需将 intern.js 配置为在后台使用 phantomjs 来使用 intern-runner 运行我们的单元测试。它是实习生世界中的“套件”(单元测试),必须在浏览器上下文中运行,例如 phantomjs。这对于需要在浏览器中进行快速启动测试但又不想在他们的桌面操作系统中启动浏览器的开发人员来说是件好事。您将需要运行 selenium-server 来驱动 phantom。所以:

1.) 独立运行 selenium-server,我在本地使用:https://www.npmjs.org/package/selenium-standalone。在单独的 cmd 控制台中启动它。

硒独立启动

2.) npm install phantomjs

3.) 在实习配置文件中执行:

     environments: [
       { browserName: 'phantomjs' }
      ],
  loader: {
        // Packages that should be registered with the loader in each testing environment
        packages: [
            { name: 'angular', location: 'bower_components/angular' },
            { name: 'angular-mocks', location: 'bower_components/angular-mocks' }
        ]

  },

4.) 运行 $node node_modules\intern\bin\intern-runner config=your/config suites=angular-test

你会是金色的。

这是我的小步单元测试,证明了角载荷。从那里,您可以使用角度模拟或其他任何方式做任何您想做的事情。

define([
        'intern/chai!expect',
        'intern!bdd',
        'intern/order!angular/angular'
    ], function (expect, bdd) {

        function inject (fn) {
            return function() {
                angular.injector(['ng']).invoke(fn);
            }
        }

        bdd.describe('Scope Test', function () {
            var ctrl, scope;

            bdd.beforeEach(inject(function ($controller, $rootScope) {
                scope = $rootScope.$new();
            }));

            bdd.it('should have an angular object', function () {

                expect(angular).to.be.an('object');
            });


            });


});

【讨论】:

  • @csnover 你能评论一下吗?
  • 你的想法是对的。如果测试需要浏览器环境(windowdocument 等),Node.js 客户端(intern-client)将无法工作。使用浏览器客户端 (client.html) 或 intern-runner 在浏览器中运行测试。鉴于原始海报使用 grunt,intern-runner 是要走的路。
  • 感谢 httpete 和 @jason0x43 的帮​​助。当我运行 4 号时,我收到以下错误:正在侦听 0.0.0.0:9000 启动隧道... 错误:[POST localhost:4444/wd/hub/session] connect ECONNREFUSED 错误:在 errnoException <904:11><895:19>
猜你喜欢
  • 1970-01-01
  • 2014-03-17
  • 1970-01-01
  • 2016-03-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
相关资源
最近更新 更多