我想为使用Angular Js的LocalStorageModule的服务编写测试,我可以在我使用的服务中添加和使用Module但是当我要测试一个简单的单元测试时,我得到了这个错误:
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,
});
};
我认为误解了如何在测试场景中加载外部模块。感谢帮助 !
答案 0 :(得分:3)
好的谢谢......解决了!
我想念>中的模块链接karma.conf.js所以模块在Angular App中工作但没有经过测试!我只需要为angular-local-storage添加正确的路径:
// 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',
'bower_components/angular-local-storage/dist/angular-local-storage.js',//< HERE
'app/scripts/**/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],