2016-06-05 8 views
0

Bei der Verwendung von Karma + Jasmin + RequireJS + AngularJS, ich bin nicht in der Lage irgendwelche meiner Beilagpolstückes Skripte zu laden, zum Beispiel angular-mocks, in den Testdaten. Die Datei scheint in Ordnung zu sein, funktioniert einfach nicht in der Spezifikation.Karma + RequireJS + AngularJS, Beilagpolstückes Skripte nicht in spec Laden

UPDATE Angular ist global und die entsprechende Shim beeinflusst es nicht.

enter image description here

In Karma.conf.js, ich bin auch angular-mocks durch RequireJS geladen werden:

module.exports = function(config) { 
    config.set({ 

    // base path that will be used to resolve all patterns (eg. files, exclude) 
    basePath: '../../', 


    // frameworks to use 
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 
    frameworks: ['jasmine', 'requirejs'], 


    // list of files/patterns to load in the browser 
    files: [ 
     {pattern: 'node_modules/angular-mocks/angular-mocks.js', included: false}, 
     ... 
     'test/euro-2016/main-test.js' 
    ], 


    // list of files to exclude 
    exclude: [ 
     'main/main-euro-2016.js' 
    ], 


    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
     '../../*.html': ['ng-html2js'] 
    }, 


    // 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_DEBUG, 


    // 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: ['PhantomJS'], 


    // Continuous Integration mode 
    // if true, Karma captures browsers, runs the tests and exits 
    singleRun: false, 

    // Concurrency level 
    // how many browser should be started simultaneous 
    concurrency: Infinity 
}) 

};

Die RequireJS shim in der Hauptdatei Karma main-test.js:

var tests = []; 
for (var file in window.__karma__.files) { 
    if (window.__karma__.files.hasOwnProperty(file)) { 
     if (/Spec\.js$/.test(file)) { 
      tests.push(file); 
     } 
    } 
} 

requirejs.config({ 
    // Karma serves files from '/base' 
    baseUrl: "/base", 

    paths: { 
     "angular": "vendor/angular/angular", 
     "angularMocks": "node_modules/angular-mocks/angular-mocks", 
     "jquery": "vendor/jquery/jquery.min", 
     ... 
    }, 

    shim: { 
     "angular":  { 
      exports: "angular", 
      deps:  ['jquery'] 
     }, 
     "angularMocks": { 
      exports: "angularMocks", 
      deps: ['angular'] 
     }, 
     ... 
    }, 

    // ask Require.js to load these files (all our tests) 
    deps: tests, 

    // start test run, once Require.js is done 
    callback: window.__karma__.start 
}); 

Die Spez-Datei:

define(['angular', 'modules/euro-2016/app', 'angularMocks'], function(angular, app, mocks){ 
    console.log("ANGULAR", angular); // ok 
    console.log("APP", app); // ok 
    console.log("MOCKS", mocks); // undefined 
}) 

enter image description here

Antwort

1

auf den Quellcode der Suche, die durch die Installation des NPM Paket installiert ist angular-mocks, speziell die Datei , hier ist, was ich sehe:

  1. Es gibt keine Erwähnung von irgendwo in diesem Code, daher angularMocks Export kann nicht funktionieren.

  2. Umgekehrt installiert sich das Plugin selbst als angular.mock. Früh in der Datei gibt es die Zeile:

    angular.mock = {}; 
    

    Und dann wird alles zu angular.mock hinzugefügt.

So können Sie Ihre exports und Zugriff auf das Plugin durch angular.mock entfernen. Dies sollte funktionieren:

define(['angular', 'modules/euro-2016/app', 'angularMocks'], function(angular, app){ 
    console.log("ANGULAR", angular); 
    console.log("APP", app); 
    console.log("MOCKS", angular.mock); 
}); 

Wenn Sie eine exports aus irgendeinem Grund haben muss (zum Beispiel, wenn Sie enforceDefine verwenden, die verlangt, dass alle shim haben exports Werte) Sie es angular.mock einstellen könnte.

+0

Danke Mann, du hast mir eine Menge Ärger erspart – Ozrix