2013-06-27 140 views
0

我無法使用require和karma爲我的AngularJS應用程序編寫測試。當我從業力中加載主源文件時,我隨機得到'module is not defined'錯誤,我不確定原因。AngularJS + Require + Karma - 無法運行測試

我的項目結構如下:

main 
-webapp 
-components 
    -{all my external libs} 
-js 
    -{my js files} 
test 
    -lib 
    angular-mocks.js 
    underscore.js 
    -webapp 
    karma.conf.js 
    server.js 
    test-main.js -> require js config 
    -unit 
    myTest.js 

我測試main.js定義如下 -

var tests = []; 
for (var file in window.__karma__.files) { 
if (/Test\.js$/.test(file)) { 
    tests.push(file); 
    console.log(file); 
} 
} 


requirejs.config({ 
baseUrl:'base/', 
paths:{ 
    'angular':'main/webapp/components/angular-1.0.7', 
    'jquery':'main/webapp/components/jquery', 
    'jquery-ui':'main/webapp/components/jquery-ui', 
    'lodash':'main/webapp/components/lodash', 
    'underscore':'test/webapp/lib/underscore', 
    'angular-mocks':'test/webapp/lib/angular-mocks', 
    'requireLibChecker':'test/webapp/checker/requireLibChecker', 
    'bootstrap':'main/webapp/components/bootstrap/js/bootstrap', 
    'bootstrap-datepicker':'main/webapp/components/bootstrap/js/bootstrap-datepicker', 
    'app':'main/webapp/js/mcc', 
    'helper':'main/webapp/js/helper', 
    'js':'main/webapp/js' 
}, 

shim:{ 
    'angular':{ 
     exports:'angular' 
    }, 
    'angular-mocks':{ 
     exports:'angular-mocks' 
    }, 
    'underscore':{ 
     exports:'_' 
    }, 
    'jquery-ui':{ 
     deps:['jquery'], 
     exports:'jquery-ui' 
    }, 
    'bootstrap-datepicker':{ 
     deps:['jquery', 'bootstrap'], 
     exports:'datepicker' 
    } 
}, 
priority:[ 
    "angular" 
], 

// ask Require.js to load these files (all our tests) 
deps:tests, 

priority:['angular'], 


// start test run, once Require.js is done 
callback:window.__karma__.start 
}); 

我的主要的第一源文件(mcc.js)引起的錯誤當通過業力加載和定義如下:

define("app", [ 
'angular', 
'js/controllers/overviewctrl', 
'js/controllers/topicactivityctrl', 
'js/services/transportmodel', 
'js/services/subjectmodel', 
'js/directives/controlsdirective', 
'bootstrap-datepicker', 
'jquery-ui' 
], function(angular) { 

var app = angular.module('mcc', []).config(['$routeProvider', function ($routeProvider) { 
    $routeProvider. 
     when('/overview', { 
      templateUrl:'partials/system-overview.html', 
      controller: 'OverviewController' 
     }). 
     when('/overview/:systemId', { 
      templateUrl:'partials/system-overview.html', 
      controller: 'OverviewController' 
     }). 
     otherwise({redirectTo:'/overview'}); 
}]); 

app.controller("AppController", ['$rootScope', 'transportModel', function($rootScope, transportModel) { 
    $rootScope.$on("$routeChangeError", function(event, current, previous, rejection) { 
     alert("Route Change Error: " + rejection); 
    }); 

    if ($rootScope.mcc_region === undefined) 
     $rootScope.mcc_region = "New York"; 

    transportModel.then(function(tm) { 
     $rootScope.mcc_regions = tm.regions; 
    }); 

}]); 

return app; 

}); 


require(['angular', 'helper'], function(angular, helper) { 

angular.element(document).ready(function() { 

    /* MANUALLY LOAD TO WAIT FOR APP */ 
    angular.bootstrap(document, ['mcc']); --> This line causes the error 

    /* NAVIGATE TO TOP OF PAGE FOR MOBILE DEVICES */ 
    window.scrollTo(0, 1); 


}); 

}); 

我想調試爲什麼 -
angular.bootstrap(document,['mcc']); 隨機導致的錯誤 - 也就是說,瀏覽器(chrome,phantomJS)有時候會找到這個模塊,有時候它通過業力運行時不會。如果我把它作爲一個普通的網絡應用程序運行,它總是第一次正常工作。

回答

0

我不認爲你要在執行測試時引導應用程序。你是否試圖將該模塊重構爲另一個文件?例如,當您運行應用程序時,相當於您的test-main.js的文件。

我的意思是,該模塊:

require(['angular', 'helper'], function(angular, helper) { 

    angular.element(document).ready(function() { 

     /* MANUALLY LOAD TO WAIT FOR APP */ 
     angular.bootstrap(document, ['mcc']); --> This line causes the error 

     /* NAVIGATE TO TOP OF PAGE FOR MOBILE DEVICES */ 
     window.scrollTo(0, 1); 


}); 

可能並不需要由噶都加載。