【发布时间】: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