在我们的 angularjs 项目中,我们使用 requirejs + angularAMD 来管理依赖项。该应用程序运行良好,但我们在使用 karma 运行 jasmine 测试时遇到了问题。
这是错误:
TypeError: 'undefined' is not an object (evaluating 'app.register.service')
at /home/bcatarino/Projects/spr/target-grunt/js/app/model/PressRelease.js:3
似乎我们的应用程序没有正确地注入到我们的测试中。
测试源代码:
'use strict';
define(['angularAMD', 'pressRelease'], function(angularAMD, pressRelease) {
describe('PressReleaseModel test', function () {
var scope, pressReleaseModel;
var inject = angularAMD.inject;
inject(function (pressRelease) {
pressReleaseModel = pressRelease;
});
it('Should add a non-existing recipient', function(pressRelease) {
expect(pressReleaseModel.get().recipients.length, 0);
});
});
});
PressRelease 的编码如下:
define([ 'app', 'angularAMD' ], function(app, angularAMD) {
app.register.service('pressRelease', function() {
//Stuff going on here
} );
});
所以这是app.register未定义的地方,我们无法弄清楚为什么。
根据要求,这里是 karma.conf:
module.exports = function(config) {
config.set({
basePath: '../../../',
files: [
{pattern: 'js_components/angular/angular.js', watched: false, included: false},
{pattern: 'js_components/angular-mocks/angular-mocks.js', included: false},
{pattern: 'js_components/angularAMD/angularAMD.js', included: false},
{pattern: 'js_components/angular-ui-router/release/angular-ui-router.min.js', included: false},
{pattern: 'js_components/angular-ui-utils/ui-utils.js', included: false},
{pattern: 'js/test/test-app.js', watched: true, included: false},
{pattern: 'js/app/model/PressRelease.js', watched: true, included: false},
{pattern: 'js/test/model/PressReleaseModelSpec.js', watched: true, included: false},
'js_components/requirejs/require.js',
'js/test/test-main.js'
],
exclude: [],
plugins: [
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-requirejs'
],
frameworks: ['jasmine', 'requirejs'],
junitReporter: {
outputFile: 'karma-test-results.xml',
suite: ''
},
port: 9876,
runnerPort: 9100,
browsers: ['PhantomJS', 'Chrome'],
autoWatch: true,
preprocessors: {
'template/**/*.html': ['html2js']
},
ngHtml2JsPreprocessor: {
// strip this from the file path
stripPrefix: 'web',
moduleName: 'templates'
},
logLevel : config.LOG_INFO
})
};
有人有什么想法吗?我们以此为例,但我们没有找到答案: https ://github.com/marcoslin/angularAMD
谢谢