Karma + Mocha中的角度模拟测试无法找到模块

  发布于 2023-01-11 19:36

我正在学习AJS单元测试,包括RequireJS,Karma,Mocha,Chai和angular-mocks.我在前四个方面有一些运气,但是需要进行"真正的"测试并且不能让角度模拟工作.有很多事情要发生,所以我会尽可能简洁.

测试/ 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配置.我敢肯定,我越来越angularangular-mocks,因为我已经通过这些错误已经奏效.

我的Gruntfile只有一个加载选项karma.conf.js.输出grunt karma是:

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);,那就是assert作品.我错过了什么?(如果上面没有足够的信息我很抱歉.我可以根据需要发布更多信息.)

1 个回答
  • 我挖到了angular-mocks.js.module只是一个设置inject.就其本身而言,它实际上并没有加载模块angular.module.我不必要地使用它进行"模块级"测试,在angular.module适当的地方.

    当进入实际的控制器/指令/等.测试,我需要module+ inject对.

    2023-01-11 19:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有