2014-10-21 2 views
1

Я в настоящее время пытаюсь запустить тесты с использованием кармы, но я не могу показаться, чтобы загрузить файл Test ...AngularJS, RequireJS, Karma - тестовый файл не загружается

это моя структура файла:

app 
    controllers 
     .. 
    directives 
    .. 
.. 
test 
    customersControllerTest.js 
karma.conf.js 
test-main.js 
.. 

это мой текущий файл karma.conf.js:

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: './scripts/jquery.js', included: true}, 
     {pattern: './scripts/angular.js', included: true}, 
     {pattern: './scripts/angular-mocks.js', included: true}, 
     'test-main.js', 
     {pattern: './test/*.js', included: false}, 
     {pattern: './app/**/*.js', included: false}, 
    ], 

    // list of files to exclude 
    exclude: [ 
     '**/*.css', 
     '**/bootstrap.js', 
     '**/routes.js', 
     '**/conf.js', 
     '**/app.js' 
    ], 

    // preprocess matching files before serving them to the browser 
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 
    preprocessors: { 
    }, 

    // 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', 'Chrome', 'Firefox'], 

    plugins: [ 
     'karma-*', 
    ], 

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

и мой тест-main.js файл:

var testPaths = { 
    'customersControllersTest': '../test/customersControllersTest' 
}, tests = Object.keys(window.__karma__.files).filter(function(file) { 
    return (/(spec|Test)\.js$/i.test(file)); 
}).map(function(test) { 
    var returnVal = false; 
    Object.keys(testPaths).map(function(path){ 
     if(testPaths[path] === test.replace('/base', '..').replace('.js', '')) { 
      returnVal = path; 
     } 
    }); 
    return returnVal; 
}); 

require.config({ 
    // Karma serves files under /base, which is the basePath from your config file 
    baseUrl: '/base/app', 
    paths: { 
     'jquery': '../scripts/jquery', 
     'angular': '../scripts/angular', 
     'angularMocks': '../scripts/angular-mocks', 

     // Modules 
     'servicesModule': './services/module', 
     'directivesModule': './directives/module', 
     'controllersModule': './controllers/module', 

     // Controllers 
     'testController': './controllers/testController', 
     'ordersController': './controllers/ordersController', 
     'allordersController': './controllers/allordersController', 
     'customersController': './controllers/customersController', 

     // Directives 
     'barChart': './directives/barsChartDirective', 
     'blueBarChart': './directives/blueBarsChartDirective', 

     // Services 
     'testService': './services/testService', 
     'routeResolver': './services/routeResolver', 
     'customersFactory': './services/customersFactory', 

     // Tests 
     'customersControllersTest': '../test/customersControllersTest' 
    }, 
    // angular does not support AMD out of the box, put it in a shim 
    shim: { 
     'angular': { 
      deps: ['jquery'], 
      exports: 'angular' 
     }, 
     'angularRoute': ['angular'], 
     'angularAnimate': ['angular'], 
     'angularMocks': ['angular'] 
    }, 
    // dynamically load all test files 
    deps: tests, 
    // we have to kickoff jasmine, as it is asynchronous 
    callback: window.__karma__.start() 
}); 

и мои customersControllersTest.js:

define('customersControllersTest', [], function() { 
    describe('test', function() { 
     console.log(this); 
     it('test1', function() { 
      expect('this').toEqual('this'); 
     }); 
     console.log(this); 
    }); 
}); 

Проверив в Devtools Chrome, он дает ошибку:

Error

, но когда я открываю его:

File loaded

Это может быть завершено у связаны между собой, так как я получаю следующее:

enter image description here

которые, отступлений за слова он не нашел тест, кричит об ошибке ...

Любые идеи о том, как я могу решить это?

+0

Какова цель 'map()', которая появляется после 'filter()', когда test-main.js строит список 'tests'? – artm

+0

Возможно, внутренняя карта может быть forEach, так как я просто присваиваю значение переменной, поэтому я вызываю модуль requireJS вместо самого файла (так что это требует магии). –

ответ

1

Одной из проблем является строка:

callback: window.__karma__.start() 

Это вызывает start сразу вместо того, чтобы передать его в качестве callback. Эта линия должна быть

callback: window.__karma__.start 

См. karma-requirejs README.

+0

это фактически исправило это ... Я тоже расскажу об исходном исходнике ошибки (скопировал структуру с сайта) спасибо за это :) –

Смежные вопросы