10

カルマ ジャスミンの単体テストを requirejs で構成しようとしています。しかし、実行するたびに、次のエラーが発生します。

Chrome 34.0.1847 (Mac OS X 10.9.2) ERROR
Uncaught Error: Mismatched anonymous define() module: function (angular){

 describe('Unit: Testing RequireJS', function(){
  var ctrl;
  var scope;
  var rootScope;

  beforeEach(angular.mock.module('wsaApp'));

  beforeEach(angular.mock...<omitted>...ch

以下は差分ファイルです: 仕様ファイル:

define(['angular'], function(angular){

describe('Unit: Testing RequireJS', function(){
   var ctrl;
   var scope;
   var rootScope;

beforeEach(angular.mock.module('wsaApp'));

beforeEach(angular.mock.inject(function($rootScope){
    scope = $rootScope.$new();
    rootScope = $rootScope;
}));

});
});

main.js

require.config({

paths: {
    /* ABC order */
    'angular': 'vendor/angular/1.2.0/angular.min'
},
shim: {
    'angular': { exports: 'angular' },

    'app/controllers': { deps: ['angular'] }
}
});

test-main.js

// This creates an array of all the files that Karma finds with a suffix of
// Test.js (eg utilsTest.js) to be added to the Require JS config below
var tests = [],
file;
for (file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
    if(/spec\.js$/.test(file)) {
        tests.push(file);
    }
}
}
requirejs.config({
baseUrl: '/base/public/javascripts/',  // Karma serves files from /base/<your-base-path>
    paths: {
        /* ABC order */
        'angular': 'vendor/angular/1.2.1/angular.min'

    },
    shim: {
        'angular': { exports: 'angular' },
        'app/controllers': { deps: ['angular'] },           
         },
deps: tests,  // add tests array to load our tests

callback: window.__karma__.start  // start tests once Require.js is done
});

カルマ.conf.js

//Karma configuration
module.exports = function (config) {
config.set({
// base path, that will be used to resolve files and exclude
basePath: '',

// Fix for "JASMINE is not supported anymore" warning
frameworks: ["jasmine", "requirejs"],

// list of files / patterns to load in the browser
files: [
    'vendor/angular/1.2.1/angular.js',
    'jquery-1.7.1.min.js',
    'test/spec/**/*.js',
    'test/test-main.js'
],

preprocessors: {
    'app/**/*.js': 'coverage'
},

// list of files to exclude
exclude: ['app/main.js'],

// test results reporter to use
// possible values: dots || progress || growl
reporters: ['progress', 'coverage'],

coverageReporter : {
    type: 'html',
    dir: 'coverage/'
},


// web server port
port: 9876,

// cli runner port
runnerPort: 9100,

// enable / disable colors in the output (reporters and logs)
colors: true,

// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel: config.LOG_INFO,

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

// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome'],

browserNoActivityTimeout: 100000,
// If browser does not capture in given timeout [ms], kill it
captureTimeout: 20000,

// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
  });
 }

他のスレッドで言及されているさまざまなオプションを試しましたが、何も機能しないようです。

4

2 に答える 2

9

最後に、すべての問題を解決し、requirejs 構成でジャスミン テストを正常に実行できました。カルマ構成のすべての依存関係を一番上に言及し、それらをincluded: false排他的としてマークして、テスト構成ファイルを介してrequirejsによってロードされるようにしました。

files: [
    {pattern: 'vendor/angular/1.2.1/angular.js', included: false},
    {pattern: 'vendor/angular/1.2.1/angular-mocks.js', included: false},
    {pattern: 'vendor/angular/1.2.1/angular-*.js', included: false},
    {pattern: 'vendor/bootstrap/bootstrap-*.js', included: false},
    {pattern: 'jquery-1.7.1.min.js', included: false},
    {pattern: 'app/app.js', included: false},
    {pattern: 'app/**/*.js', included: false},
    {pattern: 'test/test-config.js', included: true}]

test-config のみが karma を介してロードされ、他のすべては karma 構成に含まれますが、false としてマークされます。

また、モジュールとコントローラーが読み込まれるように、スペック ファイルに app.js を読み込む必要がありました。

define(['angular-mocks', 'jquery', 'app/app'], function(angularmocks, $, app){
describe.....
}
于 2014-05-20T08:35:42.987 に答える
0

パラメータを使用して test-main.js に必要な特定の .js ファイルをロードしているため、paths: { }それらを karma.config.js に明示的にリストする必要はありません。代わりに、 line を使用できます{ pattern: 'app/**/*.js', included: false }。それより前の行はすべて冗長です。修飾子があることを確認してくださいincluded: false。そうしないと、カルマがそれらをインラインでロードし、Uncaught Error: Mismatched anonymous define()問題が発生します

于 2015-07-23T11:00:22.337 に答える