【问题标题】:Getting Karma/Jasmine working for AngularJS project让 Karma/Jasmine 为 AngularJS 项目工作
【发布时间】:2014-03-03 19:53:37
【问题描述】:

更新

我已经解决了这个问题。我为依赖 int karma config 的 files 列表输入了错误的文件名。非常令人沮丧的是,我不得不修改核心角度文件来为我注销这些信息。详情请见my answer below

**如果有人对如何更好地调试类似问题有见解,请在下面发表评论或回答。*


我一直在为我的 Angular 项目进行测试。

我可以让 Karma/Jasmine 成功“通过”一个测试,但前提是我没有打电话给 inject

测试

describe("my filter", function() {

    beforeEach(function () {
        module('MyApp');
        // inject(function($injector) { });
    });

    // fails if `inject` line above is uncommented
    it('should be true', function() {
        expect(true).toBe(true);  
    });

    // always fails
    it('should still be true', inject(function (myFilter) {
        expect(true).toBe(true);
    }));

});

业力配置

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine'],
        files: [
            '../bower_components/angular/angular.js',
            '../bower_components/angular-mocks/angular-mocks.js',
            'path/to/a/file/I/misspelled.js',
            '../build/js/scripts.js', // my app code all concatenated, etc..
            '../app/filters/myFilter/myFilter.test.js'
        ],
        exclude: [],
        reporters: ['progress'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_DEBUG,
        autoWatch: false,
        browsers: ['Safari'],
        captureTimeout: 60000,
        singleRun: true
    });
};

Gruntfile

karma: {
    unit: {configFile: 'test/karma.config.js'}
},

我尽量将以上所有代码保持在最低限度,但如果相关,可以粘贴更多;不确定要走多远,因为我完全不知道失败点在哪里。

运行grunt:karma工作

Karma 可以正常启动 Safari,它似乎可以按预期解析我的所有文件。

但是,上面的 should still be true 测试失败了,堆栈跟踪如下:

Safari 7.0.2 (Mac OS X 10.9.2) my filter should still be true FAILED
    /Users/me/path/to/my/project/bower_components/angular/angular.js:3703:53
    forEach@[native code]
    forEach@/Users/me/path/to/my/project/bower_components/angular/angular.js:322:18
    loadModules@/Users/me/path/to/my/project/bower_components/angular/angular.js:3668:12
    createInjector@/Users/me/path/to/my/project/bower_components/angular/angular.js:3608:22
    workFn@/Users/me/path/to/my/project/bower_components/angular-mocks/angular-mocks.js:2138:60
Safari 7.0.2 (Mac OS X 10.9.2): Executed 2 of 2 (1 FAILED) (0.191 secs / 0.014 secs)

有人有什么见解吗?我一直在倾注于 Karma/Jasmine 文档和教程,并尝试了很多东西,但不知何故错过了它。我的运行理论是我在 Karma 配置中加载了错误的 文件 - 主要是因为我没有找到准确的描述 what 应该被加载做什么工作。

更新

我一直在深入研究该堆栈跟踪,并尝试进行一些调试/探索

angular-mocks.js:2138

injector = currentSpec.$injector = angular.injector(modules);

我在该行上方做了一些console.log()s,得到了以下内容;

console.log(modules); // ['ng', 'ngMock', 'MyApp']
console.log(angular.injector); // function createInjector(modulesToLoad) { ... }
console.log(angular.injector(modules)); // fails with same stack trace as above

这似乎暗示它未能注入这三个模块之一。

【问题讨论】:

    标签: angularjs gruntjs jasmine karma-runner


    【解决方案1】:

    好吧,看来我已经深究了。我最终去了堆栈跟踪中的所有文件位置,console.log'ing 几乎所有东西。我通过上面的 console.logging e 得到了一些有用的信息:

    throw $injectorMinErr('modulerr', "Failed to instantiate module {0} due to:\n{1}",
                  module, e.stack || e.message || e);
    

    似乎我实际上在包含的文件中遇到了拼写错误的问题。不知道为什么这些东西一开始就没有出现在控制台中。

    添加该日志消息给了我以下(以及更多)。

    $injector:modulerr] Failed to instantiate module myMistypedModule due to:
    [$injector:nomod] Module 'myMistypedModule' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
    http://errors.angularjs.org/1.2.15-build.2364+sha.7c34e1f/$injector/nomod?p0=angles
    

    【讨论】:

      猜你喜欢
      • 2014-09-20
      • 2014-05-04
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多