ReferenceError:模块未定义 – 使用Angular / Laravel应用程序的Karma / Jasmineconfiguration

我有一个现有的Angular / Laravel应用程序,其中Laravel充当angular度前端的API,仅提供JSON数据。 加载angular度应用程序index.php的页面目前由Laravel提供。 从那里,Angular接pipe。

我有一个非常困难的时间试图开始噶/茉莉花。 当使用karma startkarma start karma.conf.js运行我的testing时,从我的项目的根目录karma start karma.conf.js ,出现以下错误:

ReferenceError: module is not defined

全输出:

 INFO [karma]: Karma v0.12.28 server started at http://localhost:9876/ INFO [launcher]: Starting browser Chrome WARN [watcher]: Pattern "/Users/raph/coding/webroot/digitalocean/rugapp/public/rugapp/*.js" does not match any file. INFO [Chrome 39.0.2171 (Mac OS X 10.9.5)]: Connected on socket 3OCUMp_xhrGtlGHwiosO with id 7897120 Chrome 39.0.2171 (Mac OS X 10.9.5) hello world encountered a declaration exception FAILED ReferenceError: module is not defined at Suite.<anonymous> (/Users/raph/coding/webroot/digitalocean/rugapp/tests/js/test.js:3:16) at jasmineInterface.describe (/Users/raph/coding/webroot/digitalocean/rugapp/node_modules/karma-jasmine/lib/boot.js:59:18) at /Users/raph/coding/webroot/digitalocean/rugapp/tests/js/test.js:1:1 Chrome 39.0.2171 (Mac OS X 10.9.5): Executed 2 of 2 (1 FAILED) (0.005 secs / 0.003 secs) 

但是,铬的布尔斯维尔启动与以下显示:

在这里输入图像说明

我的karma.conf.js文件如下所示:

 // Karma configuration // Generated on Mon Dec 22 2014 18:13:09 GMT-0500 (EST) module.exports = function(config) { config.set({ // base path that will be used to resolve all patterns (eg. files, exclude) basePath: 'public/rugapp/', // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['jasmine'], // list of files / patterns to load in the browser files: [ '*.html', '**/*.js', '../../tests/js/test.js', '../../tests/js/angular/angular-mocks.js' ], // list of files to exclude exclude: [ ], // preprocess matching files before serving them to the browser // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { }, // test results reporter to use // possible values: 'dots', 'progress' // available reporters: https://npmjs.org/browse/keyword/karma-reporter reporters: ['progress'], // web server port port: 9876, // enable / disable colors in the output (reporters and logs) colors: true, // level of logging // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG logLevel: config.LOG_INFO, // enable / disable watching file and executing tests whenever any file changes autoWatch: true, // start these browsers // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher browsers: ['Chrome'], // Continuous Integration mode // if true, Karma captures browsers, runs the tests and exits singleRun: false }); }; 

我的package.json文件如下所示:

 { "devDependencies": { "gulp": "^3.8.8", "karma": "^0.12.28", "karma-chrome-launcher": "^0.1.7", "karma-jasmine": "^0.3.2", "laravel-elixir": "*" } } 

test.js

 describe("hello world", function() { var CreateInvoiceController; beforeEach(module("MobileAngularUiExamples")); beforeEach(inject(function($controller) { CreateInvoiceController = $controller("CreateInvoiceController"); })); describe("CreateInvoiceController", function() { it("Should say hello", function() { expect(CreateInvoiceController.message).toBe("Hello"); }); }); }); describe("true", function() { it("Should be true", function() { expect(true).toBeTruthy(); }); }); 

任何帮助将不胜感激。

也许这会帮助别人。

对我来说,解决scheme是确保angular-mocks.js在我的testing之前加载。 如果您不确定,您可以通过以下部分来控制karma.conf.js的顺序:

 // list of files / patterns to load in the browser files: [ // include files / patterns here 

接下来,为了让我的testing实际上加载我的angular度的应用程序,我不得不做以下几点:

 describe("hello world", function() { var $rootScope; var $controller; beforeEach(module("YourAppNameHere")); beforeEach(inject(function($injector) { $rootScope = $injector.get('$rootScope'); $controller = $injector.get('$controller'); $scope = $rootScope.$new(); })); beforeEach(inject(function($controller) { YourControllerHere = $controller("YourControllerHere"); })); it("Should say hello", function() { expect(YourControllerHere.message).toBe("Hello"); }); }); 

而在你的控制器中,

 app.controller('YourControllerHere', function() { this.message = "Hello"; }); 

另外,另一种方式:

 describe("YourControllerHere", function() { var $scope; var controller; beforeEach(function() { module("YourAppNameHere"); inject(function(_$rootScope_, $controller) { $scope = _$rootScope_.$new(); controller = $controller("YourControllerHere", {$scope: $scope}); }); }); it("Should say hello", function() { expect(controller.message).toBe("Hello"); }); }); 

享受testing!

错误意味着angular度无法注入您的模块。 大多数情况下,这是因为缺less对脚本文件的引用。 在这种情况下,请确保所有的脚本文件都是在[文件]configuration下的karma中定义的。 请特别注意path,因为如果您的脚本文件夹具有嵌套结构,请确保列表如此。 例如:

 Scripts/Controllers/One/1.js Scripts/Controllers/One/2.js 

可以在karma.conf.js>文件中列为:

 Scripts/Controllers/**/*.js 

只要在这里留给未来的search者。

如果你正在浏览器中直接运行angular度单位testing而没有Karma(或者在plunkr或者jsfiddle等…)那么可能是这样的

 <script src="ajax/libs/angular.js/1.2.0/angular.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.0/angular-route.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.0/angular-cookies.js"></script> <!-- The Mocha Setup goes BETWEEN angular and angular-mocks --> <script> mocha.setup({ "ui": "bdd", "reporter": "html" }); </script> <script src="ajax/libs/angular.js/1.2.0/angular-mocks.js"></script> <script src="myApp.js"></script> <script src="myTest.js"></script> <!-- test is last --> 

摩卡设置在angular度和angular度之间变化