Rap*_*nah 55 javascript angularjs karma-runner karma-jasmine
我有一个现有的Angular/Laravel应用程序,其中Laravel充当角度前端的API,仅提供JSON数据.加载角度应用程序的页面index.php目前由Laravel提供.从那里开始,Angular接管了.
我正在努力开始使用Karma/Jasmine.使用karma start或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)
Run Code Online (Sandbox Code Playgroud)
但是,chrome broswer确实会启动,显示如下:

我的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
});
};
Run Code Online (Sandbox Code Playgroud)
我的package.json文件如下所示:
{
"devDependencies": {
"gulp": "^3.8.8",
"karma": "^0.12.28",
"karma-chrome-launcher": "^0.1.7",
"karma-jasmine": "^0.3.2",
"laravel-elixir": "*"
}
}
Run Code Online (Sandbox Code Playgroud)
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();
});
});
Run Code Online (Sandbox Code Playgroud)
任何帮助将大大赞赏.
Rap*_*nah 76
也许这会对某人有所帮助.
对我来说,解决方案是确保angular-mocks.js在我的测试之前加载.如果您不确定,可以karma.conf.js在以下部分中控制订单:
// list of files / patterns to load in the browser
files: [
// include files / patterns here
Run Code Online (Sandbox Code Playgroud)
接下来,为了让我的测试实际加载我的角度应用程序,我必须执行以下操作:
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");
});
});
Run Code Online (Sandbox Code Playgroud)
在你的控制器中,
app.controller('YourControllerHere', function() {
this.message = "Hello";
});
Run Code Online (Sandbox Code Playgroud)
另外,另一种方式:
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");
});
});
Run Code Online (Sandbox Code Playgroud)
享受测试!
该错误意味着角度无法注入您的模块.大多数情况下,这是因为缺少对脚本文件的引用.在这种情况下,请确保在karma的[files]配置下定义所有脚本文件.请特别注意路径,因为如果您的脚本文件夹具有嵌套结构,请确保如此列出.例如:
Scripts/Controllers/One/1.js
Scripts/Controllers/One/2.js
Run Code Online (Sandbox Code Playgroud)
可以在karma.conf.js>文件中列出:
Scripts/Controllers/**/*.js
Run Code Online (Sandbox Code Playgroud)