Javascript 没有因果报应的道路 有人能帮我找到一种方法,在我的单元测试的主描述()中同时使用beforeach(module())和beforeach(inject())吗?
当我把两者都包括在内时Javascript 没有因果报应的道路 有人能帮我找到一种方法,在我的单元测试的主描述()中同时使用beforeach(module())和beforeach(inject())吗?,javascript,angularjs,node.js,requirejs,karma-runner,Javascript,Angularjs,Node.js,Requirejs,Karma Runner,当我把两者都包括在内时 beforeEach(module('ngRoute')); 及 在我的测试文件的main descripe()中,我得到了这个。但是,如果我删除beforeach(inject(){})代码,那么ngRoute将由karma正确加载 我查看了许多与此问题相关的其他帖子。例如: 正如上面每个问题所建议的,我已经确保angular-routes.js包含在我的index.html中,并且我还确保“ngRoute”是我的应用程序的依赖项 测试文件: define(
beforeEach(module('ngRoute'));
及
在我的测试文件的main descripe()中,我得到了这个。但是,如果我删除beforeach(inject(){})代码,那么ngRoute将由karma正确加载
我查看了许多与此问题相关的其他帖子。例如:
define(['ngMock', 'controllers/home/main-home-ctrl'], function () {
'use strict';
describe.only('Controller: MainHomeController', function () {
beforeEach(module('ngRoute'));
beforeEach(inject(function () {}));
afterEach(function() {
});
describe('', function () {
it('should ', function () {
});
});
});
});
files: [
{pattern : 'www/js/bootstrap.js', included : false },
{pattern : 'www/js/lib/*.js', included : false },
{pattern : 'www/js/controllers/*.js', included : false },
{pattern : 'www/js/controllers/**/*.js', included : false },
{pattern : 'www/js/directives/*.js', included : false },
{pattern : 'www/js/directives/**/*.js', included : false },
{pattern : 'www/js/factories/*.js', included : false },
{pattern : 'www/js/factories/**/*.js', included : false },
{pattern : 'www/js/filters/*.js', included : false },
{pattern : 'www/js/filters/**/*.js', included : false },
{pattern : 'www/js/services/*.js', included : false },
{pattern : 'www/js/services/**/*.js', included : false },
{pattern : 'www-test/lib/*.js', included : false },
{pattern : 'www-test/spec/**/*.js', included : false },
{pattern : 'www-test/spec/**/**/*.js', included : false },
'www-test/main-test.js',
'www/js/lib/angular-route.js'
],
define([
'angular',
'angular-route',
'controllers/index',
'directives/index',
'factories/index',
'filters/index',
'services/index'
], function (ng) {
'use strict';
return ng.module('app', ['ngRoute', 'app.controllers', 'app.directives', 'app.factories', 'app.filters', 'app.services']);
});
karma.config.js:
define(['ngMock', 'controllers/home/main-home-ctrl'], function () {
'use strict';
describe.only('Controller: MainHomeController', function () {
beforeEach(module('ngRoute'));
beforeEach(inject(function () {}));
afterEach(function() {
});
describe('', function () {
it('should ', function () {
});
});
});
});
files: [
{pattern : 'www/js/bootstrap.js', included : false },
{pattern : 'www/js/lib/*.js', included : false },
{pattern : 'www/js/controllers/*.js', included : false },
{pattern : 'www/js/controllers/**/*.js', included : false },
{pattern : 'www/js/directives/*.js', included : false },
{pattern : 'www/js/directives/**/*.js', included : false },
{pattern : 'www/js/factories/*.js', included : false },
{pattern : 'www/js/factories/**/*.js', included : false },
{pattern : 'www/js/filters/*.js', included : false },
{pattern : 'www/js/filters/**/*.js', included : false },
{pattern : 'www/js/services/*.js', included : false },
{pattern : 'www/js/services/**/*.js', included : false },
{pattern : 'www-test/lib/*.js', included : false },
{pattern : 'www-test/spec/**/*.js', included : false },
{pattern : 'www-test/spec/**/**/*.js', included : false },
'www-test/main-test.js',
'www/js/lib/angular-route.js'
],
define([
'angular',
'angular-route',
'controllers/index',
'directives/index',
'factories/index',
'filters/index',
'services/index'
], function (ng) {
'use strict';
return ng.module('app', ['ngRoute', 'app.controllers', 'app.directives', 'app.factories', 'app.filters', 'app.services']);
});
angular-routes.js位于“www/js/lib/angular routes.js”上,因此在karma运行我的单元测试时应该包括在内
app.js:
define(['ngMock', 'controllers/home/main-home-ctrl'], function () {
'use strict';
describe.only('Controller: MainHomeController', function () {
beforeEach(module('ngRoute'));
beforeEach(inject(function () {}));
afterEach(function() {
});
describe('', function () {
it('should ', function () {
});
});
});
});
files: [
{pattern : 'www/js/bootstrap.js', included : false },
{pattern : 'www/js/lib/*.js', included : false },
{pattern : 'www/js/controllers/*.js', included : false },
{pattern : 'www/js/controllers/**/*.js', included : false },
{pattern : 'www/js/directives/*.js', included : false },
{pattern : 'www/js/directives/**/*.js', included : false },
{pattern : 'www/js/factories/*.js', included : false },
{pattern : 'www/js/factories/**/*.js', included : false },
{pattern : 'www/js/filters/*.js', included : false },
{pattern : 'www/js/filters/**/*.js', included : false },
{pattern : 'www/js/services/*.js', included : false },
{pattern : 'www/js/services/**/*.js', included : false },
{pattern : 'www-test/lib/*.js', included : false },
{pattern : 'www-test/spec/**/*.js', included : false },
{pattern : 'www-test/spec/**/**/*.js', included : false },
'www-test/main-test.js',
'www/js/lib/angular-route.js'
],
define([
'angular',
'angular-route',
'controllers/index',
'directives/index',
'factories/index',
'filters/index',
'services/index'
], function (ng) {
'use strict';
return ng.module('app', ['ngRoute', 'app.controllers', 'app.directives', 'app.factories', 'app.filters', 'app.services']);
});
所以,我发现我的新手犯了个错误。角度路由包含在我的生产和开发代码中,但不包含在测试代码中,因为我只将该文件添加到生产和开发文件夹中 如果从未调用
inject()
,则从未将angular推入“运行”阶段,因此模块配置代码从未实际运行。这就是为什么删除inject()
时没有错误。