在e2e测试中测试路由Karma时出错

Error while testing route Karma in e2e tests

本文关键字:测试 出错 Karma e2e 路由      更新时间:2023-09-26

我正在尝试使用Karma对django/angularjs应用程序进行一些基本的端到端测试(e2e),我得到了一个奇怪的错误。

下面是我的测试代码:
# testacular-e2e.conf.js
describe('Log in tests', function() {
   it('should show the disconnected home', function() {
      browser().navigateTo('/#');
      expect(browser().location().url()).toBe('/#');
   });
});

当我运行这段代码时,得到以下输出:
[DEPRECATED ERROR - left for reference]
$>karma start testacular-e2e.conf.js[2013-04-30 22:19:26.465] [WARN] config - "/"是被代理的,你可能应该修改urlRoot以避免冲突[2013-04-30 22:19:26.467] [DEBUG] config - autoWatch设置为false,因为singleRunINFO [karma]: karma服务器从http://local.host:9876/启动INFO [launcher]:启动浏览器PhantomJSINFO [PhantomJS 1.9 (Mac)]:连接在socket id cwhp0intrana4toq0hks上PhantomJS 1.9 (Mac)登录测试应该显示断开连接的home FAILEDTypeError: 'undefined'不是一个函数(求值'$document.injector()')PhantomJS 1.9 (Mac):执行1的1(1失败)(0.098秒/0.04秒)

有谁能帮忙吗?
提前感谢:)

编辑:这是我的Karma配置文件:

// Karma configuration
// base path, that will be used to resolve files and exclude
basePath = '';
// frameworks to use
frameworks = ['jasmine', 'ng-scenario'];
// list of files / patterns to load in the browser
files = [
  'app/components/angular/angular.js',
  'app/components/angular-complete/angular-resource.js',
  'app/components/angular-complete/angular-cookies.js',
  'app/components/angular-mocks/angular-mocks.js',
  'app/components/ng-translate/translate.js',
  'app/scripts/*.js',
  'app/scripts/services/services.js',
  'app/scripts/**/*.js',
  'test/spec/**/*.js'
];
// list of files to exclude
exclude = [];
// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters = ['progress'];
// web server port
port = 9876;
// cli runner port
runnerPort = 9100;

// enable / disable colors in the output (reporters and logs)
colors = true;

// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_DEBUG;

// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers = ['PhantomJS'];
// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;
// plugins to load
plugins = [
  'karma-jasmine',
  'karma-phantomjs-launcher',
  'karma-chrome-launcher',
  'karma-ng-scenario',
];
编辑2:添加组件。json文件
# component.json
  {
    "name": "myapp",
    "version": "0.0.0",
    "dependencies": {
    "angular": "~1.0.5",
    "json3": "~3.2.4",
    "es5-shim": "~2.0.5",
    "angular-resource": "~1.0.5",
    "angular-cookies": "~1.0.5",
    "angular-complete" : "*",
    "jquery" : "~1.9",
    "bootstrap" : "~2.3.1",
    "bootstrap-js" : "*",
    "jquery.localScroll" : "",
    "jquery.scrollTo" : "1.4.4",
    "ng-translate" : ""
  },
  "devDependencies": {
    "angular-mocks": "~1.0.5",
    "angular-scenario": "~1.0.5"
  }
}

编辑3:更新以上Karma配置文件+这是我的e2e配置文件:

// e2e karma configuration
var fs = require('fs');
// Load from basic karma configuration
eval(fs.readFileSync('karma.conf.js')+'');
// list of files / patterns to load in the browser
files = [
  'app/components/angular/angular.js',
  'app/components/angular-complete/angular-resource.js',
  'app/components/angular-complete/angular-cookies.js',
  'app/components/angular-mocks/angular-mocks.js',
  'app/components/ng-translate/translate.js',
  'app/scripts/*.js',
  'app/scripts/services/services.js',
  'app/scripts/**/*.js',
  'test/e2e/**/*.js'
];

// Proxy the root path to the location of app server
proxies = {
  '/': 'http://localhost:8000/'
};

如果您需要"files"参数中导入的文件的详细信息,请告诉我。在实际配置中,"正常"测试(不是e2e测试)工作良好,但e2e测试给了我以下错误:

$>karma start karma-e2e.conf.js
[2013-05-01 11:32:19.047] [WARN] config - "/" is proxied, you should probably change urlRoot to avoid conflicts
INFO [karma]: Karma v0.9.2 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9 (Mac)]: Connected on socket id HmPYnc9PJUdaGb35pl-K
PhantomJS 1.9 (Mac) Log in tests should show the disconnected home FAILED
ReferenceError: Can't find variable: browser
    at /Users/hugo/Projs/wc/angular/test/e2e/scenarios.js:9
    at /Users/hugo/Projs/wc/angular/node_modules/karma-jasmine/lib/adapter.js:107
    at http://localhost:9876/karma.js:111
    at http://localhost:9876/context.html:68
PhantomJS 1.9 (Mac): Executed 1 of 1 (1 FAILED) (0.082 secs / 0.006 secs)

将此添加到您的e2e配置文件:

urlRoot = '/_karma_/';

所以我使用这个确切的配置…然后我意识到茉莉没有Browser的概念。只要我从框架中删除了jasmine,这个错误就消失了。

当然,我有另一个,所以去调试它:)

如果你正在使用requires并手动引导页面,请确保你的HTML文件中仍然有ng-app指令。

karma 0.9.5requirejs 2.1.6angularjs 1.0.7karma-ng-scenario 0.0.2karma-chrome-launcher 0.0.2

同样,这是我在localhost上运行web服务器的端到端测试的配置:8000:

module.exports = function(config) {
    config.set({
        frameworks: ['ng-scenario'],
        files: [
            'test/e2e/**/*.js'
        ],
        basePath: '../',
        autoWatch: true,
        proxies: {
            '/': 'http://localhost:8000/'
        },
        urlRoot: '__karma__',
        browsers: ['Chrome'],
        reporters: ['dots'],
        plugins: [
            'karma-ng-scenario',
            'karma-chrome-launcher'
        ]
    });
};

编辑:我用我现在的工作配置多玩了一点,以重现错误。当我导航到应用程序根目录后没有sleep()时,再次发生了这种情况。所以这可能也值得检查。

你用的是哪个版本的karma ?当我运行你的测试时,我看到一个类似的错误:

test/e2e/scenarios.js:9:14: TypeError: Object [object Object] 
has no method 'injector'

所以这不仅仅是你的环境。这看起来类似于:

https://github.com/angular/angular.js/issues/1518

vojtajina建议通过更新karma来获得最新的修复。

作为一个警告,值得注意的是,如果你用npm拉karma@canary,在你运行任何东西之前,你将需要做一些配置更改。