【发布时间】:2014-12-30 22:01:49
【问题描述】:
我正在尝试为使用 LocalStorageModule for Angular Js 的服务编写测试,我可以在我使用的服务中添加和使用模块,但是当我要测试一个简单的单元测试时,我得到了这个错误:
Error: [$injector:modulerr] Failed to instantiate module testApp due to:
Error: [$injector:modulerr] Failed to instantiate module LocalStorageModule due to:
Error: [$injector:nomod] Module 'LocalStorageModule' 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.3.8/$injector/nomod?p0=LocalStorageModule
这里是我如何尝试编写简单的测试:
'use strict';
describe('Service: AService', function () {
// load the service's module
beforeEach(module('testApp'));
// instantiate service
var AService;
// ---------------------------------- BeforeEach ------------------------------
beforeEach(inject(function (_AService_) {
// inject the AService
AService = _AService_;
}));
// ------------------------------------ Tests ---------------------------------
it('Should ..... ', function () {
// expect(!!AService).toBe(true);
expect(true).toBe(true);
});
});
服务中的模块(服务很好):
angular.module('testApp')
.factory('AService', function (localStorageService) {
return {
a_method: function(){
return = localStorageService.get('first_value');
}
});
在 app.js 我有模块注入:
angular
.module('testApp', [
'ngResource',
'ngRoute',
'LocalStorageModule'
]) ...
karma.conf.js
module.exports = function(config) {
config.set({
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// base path, that will be used to resolve files and exclude
basePath: '../',
// testing framework to use (jasmine/mocha/qunit/...)
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-route/angular-route.js',
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],
// list of files / patterns to exclude
exclude: [],
// web server port
port: 8080,
browsers: [
'PhantomJS'
],
// Which plugins to enable
plugins: [
'karma-phantomjs-launcher',
'karma-jasmine'
],
singleRun: false,
colors: true,
logLevel: config.LOG_INFO,
});
};
我认为误解了如何在测试场景中加载外部模块。感谢帮助 !
【问题讨论】:
-
在您的测试中,您正在实例化
testApp模块,但实际代码中有testApp和testAuthApp。是哪个? -
只有复制和粘贴错误!我的意思是检查我是否在测试文件中犯了一些错误,因为只有测试失败......
-
在要加载的 JS 文件列表中(在 html 测试文件中,或在 karma 配置中)是否有定义 LocalStorageModule 的 JS 文件?
-
JB 你能给我举个例子吗?我使用 Yeoman 搭建脚手架,并且 karma 配置是(添加到初始帖子)!
-
你为什么不回答我的问题?定义模块
LocalStorageModule的JS文件在哪里?
标签: angularjs angular-local-storage