我有一个现有的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)
但是,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
});
};
我的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();
});
});
任何帮助都非常赞赏。
答案 0 :(得分:75)
也许这会对某人有所帮助。
对我来说,解决方案是确保在我的测试之前加载angular-mocks.js
。如果您不确定,可以在以下部分的karma.conf.js
中控制订单:
// list of files / patterns to load in the browser
files: [
// include files / patterns here
接下来,为了让我的测试实际加载我的角度应用程序,我必须执行以下操作:
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");
});
});
享受测试!
答案 1 :(得分:5)
错误表示角度无法注入模块。大多数情况下,这是因为缺少对脚本文件的引用。在这种情况下,请确保在karma的[files]配置下定义所有脚本文件。请特别注意路径,因为如果您的脚本文件夹具有嵌套结构,请确保如此列出。例如:
Scripts/Controllers/One/1.js
Scripts/Controllers/One/2.js
可以在karma.conf.js&gt;文件中列为:
Scripts/Controllers/**/*.js
答案 2 :(得分:4)
请将此留给未来的搜索者。
如果你在没有Karma的情况下直接在浏览器中运行角度单位测试(或者在plunkr或jsfiddle等...)那么它可能就是
<script src="https://cdnjs.cloudflare.com/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="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.0/angular-mocks.js"></script>
<script src="myApp.js"></script>
<script src="myTest.js"></script> <!-- test is last -->
摩卡设置进入角度和角度模拟之间
答案 3 :(得分:1)
我遇到了类似的消息,结果发现我的angular-mocks
文件路径错误了。我使用npm来安装angular
和angular-mocks
,我在我的Karma.conf.js
中错误地指定了他们的路径:
files: [
'node_modules/angular/angular.js',
'node_modules/angular/angular-mocks.js',
'scripts/*.js',
'tests/*.js'
],
我应该指定angular-mocks.js
的路径:
'node_modules/angular-mocks/angular-mocks.js'
非常简单的错误,但如果您刚刚开始使用AngularJS单元测试并且不知道在哪里查找,则可能很费时。