Question

J'apprends les tests unitaires AJS, avec RequireJS, Karma, Mocha, Chai et Angular-Mocks.J'ai eu un peu de chance avec les quatre premiers, mais j'ai besoin de me lancer dans des tests "réels" et je n'arrive pas à faire fonctionner les simulations angulaires.Il se passe beaucoup de choses, je serai donc aussi succinct que possible.

test/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
    });
};

test/unit/mocha.conf.js

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

test/unité/MonTest.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
    });
});

Je vais ignorer la publication de toute la configuration RequireJS.Je suis presque sûr que je reçois angular et angular-mocks, parce que j'ai déjà résolu ces erreurs.

Mon Gruntfile a simplement une option à charger karma.conf.js.La sortie de grunt karma est:

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.

Si je change angular.mock.module(MODULE_NAME); à angular.module(MODULE_NAME);, le assert travaux.Qu'est-ce que je rate?(Je m'excuse s'il n'y a pas assez d'informations ci-dessus.Je peux en publier davantage, si nécessaire.)

Était-ce utile?

La solution

J'ai creusé dans angulaire-mocks.js. module est juste une configuration pour inject.À lui seul, il ne charge pas réellement de module, comme angular.module fait.Je l'utilisais inutilement pour des tests "au niveau du module", où angular.module est approprié.

Lorsque vous accédez au contrôleur/directive/etc.test, j'aurai besoin du module+inject paire.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top