噶+摩卡角模拟测试找不到模块 [英] angular-mock testing in Karma+Mocha can't find module

查看:164
本文介绍了噶+摩卡角模拟测试找不到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习AJS单元测试,与RequireJS,因果报应,摩卡,湾仔及角嘲笑。我有一些运气的第4位,但需要进入真正的测试并不能获得角嘲笑工作。有是怎么回事了很多,所以我会尽可能简洁。

I'm learning AJS unit testing, with RequireJS, Karma, Mocha, Chai and angular-mocks. I've had some luck with the first four, but need to get into "real" testing and can't get angular-mocks to work. There's a lot going on, so I'll be as succinct as possible.

测试/ karma.conf.js

module.exports = function (config) {
    config.set({
        // requirejs may need to be listed before other modules (see https://github.com/princed/karma-chai-plugins#limited-requirejs-support)
        frameworks: ["requirejs", "mocha", "chai"],

        files: [
            // load the RequireJS config files first
            {pattern: "client/app/require-shared.js",                                     watched: false},
            {pattern: "test/require-test.js",                                             watched: false},

            // set included to false for files to be loaded via RequireJS
            {pattern: "client/**/*.js",                                  included: false                },
            {pattern: "bower_components/**/*.js",                        included: false, watched: false},

            // Mocha stuff
            {pattern: "test/unit/mocha.conf.js",                                          watched: false},

            // test files
            {pattern: "test/unit/**/pageSelectorTest.js",                           included: false                }
        ],
        exclude: [
            "client/app/bootstrap.js",
            "client/app/require-main.js",
            "*.conf.js"
        ],

        reporters: ["spec"],

        // enable / disable watching file and executing tests whenever any file changes
        autoWatch: false,

        browsers: [
            "PhantomJS"
        ],

        singleRun: true
    });
};

测试/单位/ mocha.conf.js

window.mocha.setup({
    ui: "tdd"
});

测试/单位/ MyTest.js

define([
     "angular-mocks"
    ,"app"
], function () {
    "use strict";

    var MODULE_NAME = "PageSelector";
    var assert = chai.assert;


    suite("Unit testing " + MODULE_NAME, function() {
        suite(MODULE_NAME + " module", function () {
            var appModule;

            setup(function () {
                // "ng" and "ngMock" modules automatically loaded
                appModule = angular.mock.module(MODULE_NAME);
                console.log(appModule);
            });

//          setup(function () {
//              angular.mock.inject(function () {
//
//              });
//          });

            test("should exist", function () {
                assert.isDefined(appModule, "module exists");
            });
        }); // end module tests
    });
});

我将跳过发布的所有RequireJS配置。我是pretty确保我得到角嘲笑,因为我已经通过工作这些错误了。

I'll skip posting all the RequireJS config. I'm pretty sure I'm getting angular and angular-mocks, because I've worked through those errors already.

我Gruntfile只是有一个选项加载 karma.conf.js 咕噜人缘的输出是:

My Gruntfile simply has an option to load karma.conf.js. The output of grunt karma is:

Running "karma:unit" (karma) task
INFO [karma]: Karma v0.12.16 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Linux)]: Connected on socket WJUF8xogEo-XCG-8zzJX with id 10483911
PhantomJS 1.9.7 (Linux)
LOG LOG: undefined

  Unit testing PageSelector
    PageSelector module
      ✗ should exist
        AssertionError: module exists: expected undefined to not equal undefined
            at /home/client/node_modules/chai/chai.js:925
            at assertEqual (/home/client/node_modules/chai/chai.js:1402)
            at /home/client/node_modules/chai/chai.js:3627
            at /home/client/node_modules/chai/chai.js:2648
            at /home/client/test/unit/utility/pageSelectorTest.js:37
            at callFn (/home/client/node_modules/mocha/mocha.js:4338)
            at /home/client/node_modules/mocha/mocha.js:4331
            at /home/client/node_modules/mocha/mocha.js:4728
            at /home/client/node_modules/mocha/mocha.js:4819
            at next (/home/client/node_modules/mocha/mocha.js:4653)
            at /home/client/node_modules/mocha/mocha.js:4663
            at next (/home/client/node_modules/mocha/mocha.js:4601)
            at /home/client/node_modules/mocha/mocha.js:4625
            at done (/home/client/node_modules/mocha/mocha.js:4300)
            at callFn (/home/client/node_modules/mocha/mocha.js:4343)
            at /home/client/node_modules/mocha/mocha.js:4331
            at next (/home/client/node_modules/mocha/mocha.js:4626)
            at /home/client/node_modules/mocha/mocha.js:4625
            at done (/home/client/node_modules/mocha/mocha.js:4300)
            at callFn (/home/client/node_modules/mocha/mocha.js:4343)
            at /home/client/node_modules/mocha/mocha.js:4331
            at next (/home/client/node_modules/mocha/mocha.js:4626)
            at /home/client/node_modules/mocha/mocha.js:4630
            at timeslice (/home/client/node_modules/mocha/mocha.js:5763)


PhantomJS 1.9.7 (Linux): Executed 1 of 1 (1 FAILED) ERROR (0.003 secs / 0.002 secs)

Warning: Task "karma:unit" failed. Use --force to continue.

Aborted due to warnings.

如果我更改 angular.mock.module(MODULE_NAME); angular.module(MODULE_NAME); ,在断言的作品。我在想什么? (我很抱歉,如果没有足够的信息之上。我可以发布更多,根据需要)。

If I change angular.mock.module(MODULE_NAME); to angular.module(MODULE_NAME);, the assert works. What am I missing? (I apologize if there isn't enough info above. I can post more, as needed.)

推荐答案

我挖成角mocks.js。 模块只是一个设置为。就其本身而言,它实际上并没有加载模块,如 angular.module 一样。我用它为不必要的模块级的测试,其中 angular.module 是合适的。

I dug into angular-mocks.js. module is just a set-up for inject. On its own, it doesn't actually load a module, like angular.module does. I was using it unnecessarily for "module-level" testing, where angular.module is appropriate.

当获得到实际控制/指令/等。测试中,我需要的模块 +

When getting in to actual controller/directive/etc. testing, I'll need the module+inject pair.

这篇关于噶+摩卡角模拟测试找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆