我正在尝试对我在 Angular JS 中创建的工厂运行一些 Jasmine 单元测试。
这是我的 app.js
var app = angular.module('myApp', [
'ngRoute'
]);
这是 services.js
app.factory('Service', function(){
var service = {
one: function(){
return 1;
}
};
return service;
})
这是 Karma 配置文件
files: [
'public/bower_components/angular/angular.js',
'public/bower_components/angular-mocks/angular-mocks.js',
'public/js/app.js',
'public/js/controllers.js',
'public/js/services.js',
'test/**/*test.js'
],
这是测试
'使用严格';
(function () {
describe('myApp', function () {
beforeEach(module('myApp'));
it('testing the service', inject(function (Service) {
console.log(Service)
}));
});
})();
我真的被困住了,不知道现在该走哪条路:( 所有正确的脚本都加载到浏览器中。此外,该应用程序在浏览器中以正常模式运行,因此服务工厂没问题。
错误信息说:
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngRoute due to:
Error: [$injector:nomod] Module 'ngRoute' 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.
最佳答案
问题正是消息所说的:ngRoute 不可用,因为您没有将该脚本包含在您的 Karma 配置文件中。您需要在文件数组中添加这样一行:
'public/bower_components/angular-route/angular-route.js',
我不知道路径是什么,所以你可能需要调整它。
关于javascript - 将 angular js 服务/工厂注入(inject) Jasmine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23770376/