2014-09-26 102 views
2

我想設置Intern.io所以我可以測試我的角應用程序。我也在使用Grunt和Bower。我只是試圖讓準系統自動執行測試任務。我的問題,當我嘗試運行測試是我收到以下錯誤信息:越來越角和intern.io一起工作

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/ 
}); 

基本上,運行測試時,如何獲得在該文件中定義的窗口?

+0

行24699明確地說:})(window,document);所以它期望發送到iffy的窗口上下文,並且node.js上下文中沒有窗口。 – httpete 2014-09-27 14:20:33

+0

我認爲你的意思是「intern.js」@Evan – httpete 2014-09-28 12:47:05

回答

5

我認爲這是對intern.js如何看待測試環境的期待。 Angular固有地運行在瀏覽器上下文中,所以它需要一個全局窗口。 24699行的主要iffe確實(窗口)。我們必須使用intern-runner而不是intern-client,這樣我們可以驅動一個具有窗口上下文的瀏覽器。

但是你可能想要像Karma例子所顯示的那樣無頭快速地運行。所以,我們簡單地配置intern.js來使用引擎蓋下的phantomjs來運行我們的單元測試和intern-runner。它是實習生世界中的一個「套件」(單元測試),必須在瀏覽器環境中運行,例如phantomjs。這對於需要在瀏覽器中進行快速防火測試的開發人員來說是很好的,但不希望在桌面操作系統中啓動瀏覽器。您將需要硒服務器運行驅動幻影。所以:

1.)運行selenium-server standalone,我在本地使用:https://www.npmjs.org/package/selenium-standalone。在獨立的cmd控制檯中啓動。

selenium-standalone start

2)NPM安裝phantomjs

3)在實習生-config文件做:

 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_modules \實習生\ BIN \實習生亞軍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'); 
      }); 


      }); 


}); 
+0

@csnover你可以評論嗎? – httpete 2014-09-27 17:15:29

+0

你有正確的想法。如果測試需要瀏覽器環境('window','document'等),則Node.js客戶端('intern-client')將不起作用。使用瀏覽器客戶端(client.html)或'intern-runner'在瀏覽器中運行測試。考慮到原始海報使用的是咕嚕聲,「實習生 - 奔跑者」就是要走的路。 – jason0x43 2014-09-28 21:14:20

+0

感謝httpete和@ jason0x43的幫助。當我運行編號4時,出現以下錯誤:正在監聽0.0.0.0:9000 正在啓動隧道... 錯誤:[POST http:// localhost:4444/wd/hub/session]連接ECONNREFUSED 錯誤:connect ECONNREFUSED at errnoException at Object.afterConnect [as oncomplete] 總計:測試0個平臺,0/0測試失敗;致命錯誤發生 – Evan 2014-09-30 17:01:10