2016-08-05 75 views
0

这是我karma.conf.js角单元测试失败,未能够注入servies

`use strict`; 

var path = require('path'); 
var conf = require('./gulp/conf'); 

var _ = require('lodash'); 
var wiredep = require('wiredep'); 

var pathSrcHtml = [ 
    path.join(conf.paths.src, '/**/*.html') 
]; 

function listFiles() { 
    var wiredepOptions = wiredep({ 
     directory: 'src/bower_components', 
     exclude: [], 
     dependencies: true, 
     devDependencies: true 
    }); 

    return wiredep(wiredepOptions).js 
    .concat([ 
     path.join(conf.paths.src, '/app/**/*.module.js'), 
     path.join(conf.paths.src, '/app/**/*.js'), 
     path.join(conf.paths.src, '/app/*.spec.js') 
    ]) 
    .concat(pathSrcHtml); 
} 

module.exports = function(config) { 
    var files=['https://maps.googleapis.com/maps/api/js?sensor=false'].concat(listFiles()); 
    var configuration = { 
    files: files, 

    singleRun: true, 

    autoWatch: false, 

    ngHtml2JsPreprocessor: { 
     stripPrefix: conf.paths.src , 
     moduleName: 'truelocal' 
    }, 

    logLevel: 'WARN', 

    frameworks: ['jasmine', 'angular-filesort'], 

    angularFilesort: { 
     whitelist: [path.join(conf.paths.src, '/**/!(*.html|*.spec|*.mock).js')] 
    }, 

    browsers : ['PhantomJS'], 

    plugins : [ 
     'karma-phantomjs-launcher', 
     'karma-angular-filesort', 
     'karma-coverage', 
     'karma-jasmine', 
     'karma-ng-html2js-preprocessor' 
    ], 

    coverageReporter: { 
     type : 'html', 
     dir : 'coverage/' 
    }, 
    // level of logging 
     // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 
     logLevel: config.LOG_DEBUG, 

     captureConsole: true, 
    reporters: ['progress'] 
    }; 

    // This is the default preprocessors configuration for a usage with Karma cli 
    // The coverage preprocessor in added in gulp/unit-test.js only for single tests 
    // It was not possible to do it there because karma doesn't let us now if we are 
    // running a single test or not 
    configuration.preprocessors = {}; 
    pathSrcHtml.forEach(function(path) { 
    configuration.preprocessors[path] = ['ng-html2js']; 
    }); 

    // This block is needed to execute Chrome on Travis 
    // If you ever plan to use Chrome and Travis, you can keep it 
    // If not, you can safely remove it 
    // https://github.com/karma-runner/karma/issues/1144#issuecomment-53633076 
    if(configuration.browsers[0] === 'Chrome' && process.env.TRAVIS) { 
    configuration.customLaunchers = { 
     'chrome-travis-ci': { 
     base: 'Chrome', 
     flags: ['--no-sandbox'] 
     } 
    }; 
    configuration.browsers = ['chrome-travis-ci']; 
    } 

    config.set(configuration); 
}; 

//Bower.json 
{ 
    "name": "truelocal", 
    "version": "0.0.0", 
    "dependencies": { 
    "angular": "~1.4.10", 
    "angular-animate": "~1.4.10", 
    "angular-aria": "~1.4.10", 
    "angular-bootstrap": "~0.13.4", 
    "angular-cookies": "~1.4.10", 
    "angular-local-storage": "~0.2.3", 
    "angular-messages": "~1.4.10", 
    "angular-resource": "~1.4.10", 
    "angular-sanitize": "~1.4.10", 
    "angular-toastr": "~1.5.0", 
    "angular-touch": "~1.4.10", 
    "animate.css": "~3.4.0", 
    "bootstrap": "~3.3.5", 
    "jquery": "~1.11.3", 
    "malarkey": "yuanqing/malarkey#~1.3.1", 
    "moment": "~2.10.6", 
    "moment-parseformat": "~1.1.3", 
    "ng-file-upload-shim": "~9.1.2", 
    "jQuery.dotdotdot": "~1.7.4", 
    "lodash": "3.10.1", 
    "angular-ui-router": "~0.2.18", 
    "markerclustererplus": "~2.1.4", 
    "ng-fastclick": "~1.0.2", 
    "angular-swipe": "~0.1.0", 
    "angular-bindonce": "~0.3.3", 
    "angular-loading-bar": "~0.9.0", 
    "jquery-placeholder": "~2.3.1", 
    "angular-shims-placeholder": "^0.4.7" 
    }, 
    "devDependencies": { 
    "angular-mocks": "~1.4.10" 
    }, 
    "overrides": { 
    "bootstrap": { 
     "main": [ 
     "dist/css/bootstrap.css", 
     "dist/fonts/glyphicons-halflings-regular.eot", 
     "dist/fonts/glyphicons-halflings-regular.svg", 
     "dist/fonts/glyphicons-halflings-regular.ttf", 
     "dist/fonts/glyphicons-halflings-regular.woff", 
     "dist/fonts/glyphicons-halflings-regular.woff2" 
     ] 
    } 
    }, 
    "resolutions": { 
    "jquery": "~1.11.3", 
    "angular": "^1.5.7" 
    } 
} 

这是一家拥有超过200个单元测试现有项目写的,代码可能需要一些更新,但我无法用依赖服务执行任何单元测试。 我猜angular-mocks导致了一个问题,如果我把它在依赖项中移动,我不会得到模块丢失的错误。但我仍然没有得到单元测试中的其他依赖。

+0

我无法一次运行所有测试,TypeError:undefined不是一个对象(评估'errorService.service')src/app/services/error.service.spec.js中'keep on在单元测试中得到这个错误以便注入任何东西 – user6680441

回答

0

Karma.conf.js缺少项目中的一个文件。一旦所有文件都被送入测试配置文件。测试像魅力一样工作!