2

我完成了从this codelab开始的教程,并按照建议尝试修复单元测试中的错误。 但我不知道如何解决这个错误。我已经看过这个问题:running-tests-with-localstorage但它没有解决我的问题。Yeoman的Angular.js codelab - 修复测试

这是我的测试文件:

'use strict'; 

describe('Controller: MainCtrl', function() { 

    // load the controller's module 
    beforeEach(function(){ 
    module('mytodoApp'), 
    module('LocalStorageModule') 
    }); 

    var MainCtrl, 
    scope; 

    // Initialize the controller and a mock scope 
    beforeEach(inject(function ($controller, $rootScope) { 
    scope = $rootScope.$new(); 
    MainCtrl = $controller('MainCtrl', { 
     $scope: scope 
    }); 
    })); 

    it('should have no items to start', function() { 
    expect(scope.todos.length).toBe(0); 
    }); 
}); 

这里是控制台输出:

PhantomJS 1.9.7 (Linux) Controller: MainCtrl should have no items to start FAILED 
     Error: [$injector:modulerr] Failed to instantiate module mytodoApp 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.2.16/$injector/nomod?p0=LocalStorageModule 
      at /home/ubuntu/mytodo/bower_components/angular/angular.js:1613 
      at ensure (/home/ubuntu/mytodo/bower_components/angular/angular.js:1535) 
      at module (/home/ubuntu/mytodo/bower_components/angular/angular.js:1823) 
      at /home/ubuntu/mytodo/bower_components/angular/angular.js:3781 
     http://errors.angularjs.org/1.2.16/$injector/modulerr?p0=LocalStorageModule&p1=Error%3A%20%5B%24injector%3Anomod%5D%20Module%20'LocalStorageModule'%20is%20not%20available!%20You%20either%20misspelled%20the%20module%20name%20or%20forgot%20to%20load%20it.%20If%20registering%20a%20module%20ensure%20that%20you%20specify%20the%20dependencies%20as%20the%20second%20argument.%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.2.16%2F%24injector%2Fnomod%3Fp0%3DLocalStorageModule%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F7dcf1b25480258d399759429338cedc57239f2d1%3A1613%0A%20%20%20%20at%20ensure%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F7dcf1b25480258d399759429338cedc57239f2d1%3A1535)%0A%20%20%20%20at%20module%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F7dcf1b25480258d399759429338cedc57239f2d1%3A1823)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F7dcf1b25480258d399759429338cedc57239f2d1%3A3781 
      at /home/ubuntu/mytodo/bower_components/angular/angular.js:3810 
     http://errors.angularjs.org/1.2.16/$injector/modulerr?p0=mytodoApp&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20Failed%20to%20instantiate%20module%20LocalStorageModule%20due%20to%3A%0AError%3A%20%5B%24injector%3Anomod%5D%20Module%20'LocalStorageModule'%20is%20not%20available!%20You%20either%20misspelled%20the%20module%20name%20or%20forgot%20to%20load%20it.%20If%20registering%20a%20module%20ensure%20that%20you%20specify%20the%20dependencies%20as%20the%20second%20argument.%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.2.16%2F%24injector%2Fnomod%3Fp0%3DLocalStorageModule%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F7dcf1b25480258d399759429338cedc57239f2d1%3A1613%0A%20%20%20%20at%20ensure%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F7dcf1b25480258d399759429338cedc57239f2d1%3A1535)%0A%20%20%20%20at%20module%20(http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F7dcf1b25480258d399759429338cedc57239f2d1%3A1823)%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F7dcf1b25480258d399759429338cedc57239f2d1%3A3781%0Ahttp%3A%2F%2Ferrors.angularjs.org%2F1.2.16%2F%24injector%2Fmodulerr%3Fp0%3DLocalStorageModule%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520Module%2520'LocalStorageModule'%2520is%2520not%2520available!%2520You%2520either%2520misspelled%2520the%2520module%2520name%2520or%2520forgot%2520to%2520load%2520it.%2520If%2520registering%2520a%2520module%2520ensure%2520that%2520you%2520specify%2520the%2520dependencies%2520as%2520the%2520second%2520argument.%250Ahttp%253A%252F%252Ferrors.angularjs.org%252F1.2.16%252F%2524injector%252Fnomod%253Fp0%253DLocalStorageModule%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A8080%252Fbase%252Fbower_components%252Fangular%252Fangular.js%253F7dcf1b25480258d399759429338cedc57239f2d1%253A1613%250A%2520%2520%2520%2520at%2520ensure%2520(http%253A%252F%252Flocalhost%253A8080%252Fbase%252Fbower_components%252Fangular%252Fangular.js%253F7dcf1b25480258d399759429338cedc57239f2d1%253A1535)%250A%2520%2520%2520%2520at%2520module%2520(http%253A%252F%252Flocalhost%253A8080%252Fbase%252Fbower_components%252Fangular%252Fangular.js%253F7dcf1b25480258d399759429338cedc57239f2d1%253A1823)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A8080%252Fbase%252Fbower_components%252Fangular%252Fangular.js%253F7dcf1b25480258d399759429338cedc57239f2d1%253A3781%0A%20%20%20%20at%20http%3A%2F%2Flocalhost%3A8080%2Fbase%2Fbower_components%2Fangular%2Fangular.js%3F7dcf1b25480258d399759429338cedc57239f2d1%3A3810 
      at /home/ubuntu/mytodo/bower_components/angular/angular.js:3810 
PhantomJS 1.9.7 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.04 secs/0.006 secs) 
Warning: Task "karma:unit" failed. Use --force to continue. 

Aborted due to warnings. 

更新:新增答案。

这是我的karma.conf.js文件,修复。必须将模块angular-local-storage.js添加到文件列表中。

// 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-animate/angular-animate.js', 
     'bower_components/angular-cookies/angular-cookies.js', 
     'bower_components/angular-resource/angular-resource.js', 
     'bower_components/angular-route/angular-route.js', 
     'bower_components/angular-sanitize/angular-sanitize.js', 
     'bower_components/angular-touch/angular-touch.js', 
     'bower_components/jquery/dist/jquery.js', 
     'bower_components/jquery-ui/jquery-ui.js', 
     'bower_components/angular-ui-sortable/sortable.js', 
     'bower_components/angular-local-storage/angular-local-storage.js', 
     'app/scripts/**/*.js', 
     'test/mock/**/*.js', 
     'test/spec/**/*.js' 
    ], 
+1

的错误状态,它不能加载'LocalStorageModule'模块,你已经将包含这个模块的文件包含到你的karma配置中,对吧? – klyd 2014-09-04 01:34:50

+0

你是对的。我完全忘了在Karma.conf.js文件中添加该模块。请张贴它作为答案,所以我可以标记它是正确的。 – ThiagoPonte 2014-09-04 11:07:10

回答

3

karama.conf.js文件包含一个files阵列。测试时,请确保插入到数组包含所有的依赖模块文件:

files: [ 
    /* file paths */ 
] 

另外一个很酷的事情是,文件部分支持patterns。为了避免将来出现此问题的一种方法是把所有的模块或所需的脚本在一个地方或在一个共同的文件夹,并用一个模式来加载它们所有:

files: [ 
    /* or whatever other pattern you want to use */ 
    '/myProject/scripts/vendor/*.min.js', 
    '/myProject/scripts/modules/*.min.js', 
    /* the use of ** will search any child folder for the file pattern */ 
    '/myProject/scripts/tests/**/*.js' 
]