1

我正在尝试为使用 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

这里 id 我如何尝试编写简单的测试:

'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'
 ]) ... 

业力.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,


 });
};

我认为误解了如何在测试场景中加载外部模块。感谢帮助 !

4

1 回答 1

3

好的,谢谢...解决了!

我想在 > karma.conf.js 中链接模块,因此模块在 Angular 应用程序中工作但不在测试中!我只需要将正确的路径添加到 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'

],
于 2014-12-31T15:41:00.490 回答