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