Getting Karma/Jasmine working for AngularJS project
更新:
我已经解决了这个问题。我错误地输入了依赖项 int karma 配置的
**如果有人对如何更好地调试类似问题有见解,请在下方评论或回答。*
我一直在为我的 Angular 项目进行测试。
我可以让 Karma/Jasmine 成功"通过"测试,但前提是我没有调用
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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); })); }); |
业力配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | 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 }); }; |
咕噜文件
1 2 3 | karma: { unit: {configFile: 'test/karma.config.js'} }, |
我尽量将上述所有代码保持在最低限度,但如果相关,可以粘贴更多;不太确定要走多远,因为我完全不知道故障点在哪里。
运行
Karma 很好地启动了 Safari,它似乎按预期解析了我的所有文件。
但是,上面的
1 2 3 4 5 6 7 8 | 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 配置中加载了错误的文件——主要是因为我还没有找到准确描述应该加载什么才能工作的内容。
更新
我一直在挖掘堆栈跟踪,并尝试进行一些调试/探索
1 | injector = currentSpec.$injector = angular.injector(modules); |
我在那条线上做了一些
1 2 3 | 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 |
我想这似乎意味着它未能注入这三个模块之一。
好吧,看来我已经深入了解了。我最终访问了 stacktrace 中的所有文件位置,并且
1 2 3 | throw $injectorMinErr('modulerr',"Failed to instantiate module {0} due to:\ {1}", module, e.stack || e.message || e); |
实际上,我似乎在包含的文件中存在拼写错误。不知道为什么这些东西一开始就没有出现在控制台中。
添加该日志消息给了我以下(以及更多)。
1 2 3 | $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 |