javascript - 引用错误 : module is not defined - Karma/Jasmine configuration with Angular/Laravel app

标签 javascript angularjs karma-runner karma-jasmine

我有一个现有的 Angular/Laravel 应用程序,其中 Laravel 充当仅提供 JSON 数据的 Angular 前端的 API。加载 Angular 应用程序的页面 index.php 目前由 Laravel 提供服务。从那里开始,Angular 接管了工作。

我很难尝试开始使用 Karma/Jasmine。当使用 karma startkarma start karma.conf.js 从项目的根目录运行我的测试时,出现以下错误:

ReferenceError: 模块未定义

完整输出:

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 浏览器会启动并显示以下内容:

enter image description here

我的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();
    });
});

如有任何帮助,将不胜感激

最佳答案

也许这会对某人有所帮助。

对我来说,解决方案是确保在我的测试之前加载了 angular-mocks.js。如果不确定,可以在 karma.conf.js 中的以下部分控制顺序:

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

接下来,为了让我的测试真正加载我的 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");
    });

});

在你的 Controller 中,

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");
    });

});

享受测试吧!

关于javascript - 引用错误 : module is not defined - Karma/Jasmine configuration with Angular/Laravel app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27622382/

相关文章:

javascript - 我可以使用 jquery 或 javascript 将数据连续附加到特定的 td 吗?

javascript - 在 Enzyme 描述函数中的何处挂载和卸载?

javascript - jQuery 组合框验证

javascript - node.js 正则表达式字符串参数中的匹配组不起作用

javascript - AngularJS ngTable 中每行的按钮

javascript - ng 中的多个功能 - 单击仅触发一次功能

angularjs - 无法以 Angular 启动 karma 服务器

angular - karma : Angular 4 No provider for Service

AngularJS:如何测试将焦点放在元素上的指令?

requirejs - Karma 正在使用 RequireJS 加载测试,但实际规范并未运行