2015-02-06 59 views
1

我試圖用因果報應到單元測試添加到一個角控制器,但我碰到的一個問題,我需要一些幫助。我在這裏發現了類似的問題,但沒有人讓我回答。我試着改變karma.conf.js中「files」數組的順序,以爲它們沒有被正確加載,但沒有產生任何結果。當我運行「業力開始karma.conf.js」我得到的錯誤角噶測試運行不正常,賦予錯誤「論證‘FN’不是一個函數,得到了未定義」

控制檯錯誤從因緣

Error: [$injector:modulerr] Failed to instantiate module undefined due to: 
Error: [ng:areq] Argument 'fn' is not a function, got undefined 
at assetArg (D:/xxxx/bower_components/angular/angular.js:1580 
... 
... 
... (long callstack within angular.js and angular-mocks.js) 
... 
... 
at workFn (D:/xxxx/bower_components/angular-mocks/angular-mocks.js:2172 

我只使用一個測試文件現在,這是非常基本的。模塊名稱是正確的,並且「myApp.views.view1`文件正在由因緣服務器提供服務,在‘調試’級別因緣記錄證實了這一點

view1_test.js

'use strict'; 

describe('myApp.views.view1 module', function() { 

    beforeEach(module('myApp.views.view1',[])); 

    describe('view1 controller', function(){ 

    it('should exist....', inject(function($controller) { 
     //spec body 
     var view1Ctrl = $controller('View1Ctrl'); 
     expect(view1Ctrl).toBeDefined(); 
    })); 

    }); 
}); 

而且繼承人'視圖控制器它的測試

view1.js

'use strict'; 

angular.module('myApp.views.view1', ['ngRoute']) 

.config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/view1', { 
    templateUrl: 'src/views/view1/view1.html', 
    controller: 'View1Ctrl' 
    }); 
}]) 
.controller('View1Ctrl', ['$scope','$anchorScroll',function($scope,$anchorScroll) { 
    //scroll to top of page 
    $anchorScroll(); 

    $scope.testVar = "it works, congrats!"; 


}]); 

目錄結構

/ 
.bowerrc 
.gitignore 
bower.json 
package.json 
karma.conf.js 
/sass_styles 
    base.scss 
/app 
    index.html 
    /src 
     app.js 
     /bower_components 
     /directives 
      /example 
       directive.js 
       template.html 
     /views 
      /view1 
       view1.html 
       view1.js 
       view1_test.js 
     /styles 
      base.css 

這是我karma.conf.js文件。我使用PhantomJS作爲瀏覽器和html2js預處理,包括使用「templateURL」

karma.conf.js

module.exports = function(config){ 
    config.set({ 

     basePath : './app/', 

     preprocessors: { 
      'src/directives/**/*.html': ['ng-html2js'] 
     }, 

     files : [ 
      'src/bower_components/angular/angular.js', 
      'src/bower_components/angular-route/angular-route.js', 
      'src/bower_components/angular-mocks/angular-mocks.js', 
      'src/directives/**/*.js', 
      'src/views/**/*.js', 
      //directive templates 
      'app/src/directives/**/**.html' 
     ], 

     autoWatch : true, 

     // level of logging 
     // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG 
     logLevel: config.LOG_DEBUG, 

     frameworks: ['jasmine'], 

     browsers : ['PhantomJS'], 

     plugins : [ 
       'karma-chrome-launcher', 
       'karma-firefox-launcher', 
       'karma-phantomjs-launcher', 
       'karma-jasmine', 
       'karma-junit-reporter', 
       'karma-ng-html2js-preprocessor' 
      ], 

     junitReporter : { 
      outputFile: '../test_out/unit.xml', 
      suite: 'unit' 
     }, 

     ngHtml2JsPreprocessor: { 
      // strip app from the file path 
      stripPrefix: 'app/', 
      stripSufix: '.ext', 
      // prepend this to the 
      prependPrefix: 'served/', 

      // setting this option will create only a single module that contains templates 
      // from all the files, so you can load them all with module('foo') 
      moduleName: 'templates' 
     } 


    }); 
}; 

道歉複製粘貼一噸的東西指令html的模板,但這是一個涉及的過程,我想確保我不會錯過一小部分。那麼,我在這裏做錯了什麼導致了這個錯誤?

回答

1

問題是,當您在spec文件測試設置中獲取模塊時,實際上是通過指定第二個參數來創建它,因此它只是清理在該模塊下注冊的所有內容。

變化:

beforeEach(module('myApp.views.view1',[])); 

beforeEach(module('myApp.views.view1')); 

,也當實例使用$controller您需要提供的範圍,因爲你是注射在你的控制器$scope並沒有$scopeProvider控制器存在它是一個特殊的依賴注入,所以你需要提供它。

事情是這樣的:

it('should exist....', inject(function($controller, $rootScope) { 
    //spec body 
    var scope = $rootScope.$new(); 
    var view1Ctrl = $controller('View1Ctrl', {$scope:scope}); 
    expect(view1Ctrl).toBeDefined(); 
})); 
+0

良好的漁獲物。但現在我面臨一個新的錯誤(當然) 「錯誤:[$注射器:unpr]未知的提供者:$ scopeProvier < - $ scope < - View1Ctrl」。我是否需要手動指定某處注入$ scopeProvider? – ejfrancis 2015-02-06 16:55:29

+0

您需要在當地人提供範圍。il更新我的答案 – PSL 2015-02-06 16:57:03

+0

完美,謝謝你的幫助。我與此搏鬥了一段時間 – ejfrancis 2015-02-06 17:07:48

相關問題