angularjs - 让 Karma/Jasmine 为 AngularJS 项目工作

标签 angularjs gruntjs jasmine karma-runner

更新:

我已经解决了这个问题。我输错了依赖项 int karma 配置的 files 列表的文件名。非常令人沮丧的是,我必须修改核心 Angular 文件才能为我注销此信息。参见 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);
    }));

});

karma 配置

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 配置中加载了错误的文件——主要是因为我没有找到关于应该加载什么的准确描述 什么工作。

更新

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

angular-mocks.js:2138:

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

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

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

猜测这似乎暗示它未能注入(inject)这三个模块之一。

最佳答案

好吧,看来我已经追根究底了。我最终访问了堆栈跟踪中的所有文件位置和 console.log 几乎所有内容。我通过上面的 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

关于angularjs - 让 Karma/Jasmine 为 AngularJS 项目工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22156045/

相关文章:

javascript - Protractor 页面对象错误

javascript - JS、 Angular 、AJAX : how to mix those three?

javascript - 咕噜声 : get line numbers for errors

javascript - Gruntjs 让文件消失

javascript - Grunt Karma 运行测试但阻止

angularjs - Jasmine 未定义不是对象,找不到键

Jasmine 找不到 Spec 文件

javascript - 去抖动不在 $scope.$watch 内触发

javascript - Angular JS 重定向到模块配置中的页面

javascript - 从 hh :mm:yy (24 hour) to expanded time form 开始的时间转换