标签:
http://karma-runner.github.io/0.8/plus/RequireJS.html
"karma": "^0.13.15","karma-chrome-launcher": "^0.2.1","karma-jasmine": "^0.3.6","karma-mocha": "^0.2.0", //与jasmine 二选一即可,建议使用jasmine,mocha需要其他的断言包"karma-requirejs": "^0.2.2",
karma init
// Karma configuration// Generated on Tue Nov 10 2015 09:39:31 GMT+0800 (中国标准时间)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-adapterframeworks: [‘jasmine‘, ‘requirejs‘],// 配置要加载的文件//字符串形式的表示 通过<script>标签进行加载//对象形式的必须包含pattern、included属性,pattern进行匹配文件,included为true表示通过requirejs进行加载(后面会结束配置说明),否则通过script标签进行加载files: [‘app/bower_components/angular/angular.js‘,‘app/bower_components/angular-route/angular-route.js‘,‘app/bower_components/angular-mocks/angular-mocks.js‘,‘app/components/**/*.js‘,‘app/view*/**/*.js‘,{pattern: ‘app/define/*.js‘, included: false},{pattern: ‘app/test/*.js‘, included: false},‘test-main.js‘],// list of files to excludeexclude: [‘**/*.swp‘], proxies: {
//‘/static‘: ‘http://gstatic.com‘,
‘/log‘: ‘http://localhost:3000‘ //避免跨域,测试异步请求时如果指定完整url就是跨域,会出错,如果只写路径会请求到karma服务器localhsot:9876/
},// preprocess matching files before serving them to the browser// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessorpreprocessors: {},// test results reporter to use// possible values: ‘dots‘, ‘progress‘// available reporters: https://npmjs.org/browse/keyword/karma-reporterreporters: [‘progress‘],// web server portport: 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_DEBUGlogLevel: config.LOG_INFO,// enable / disable watching file and executing tests whenever any file changesautoWatch: true,// start these browsers// available browser launchers: https://npmjs.org/browse/keyword/karma-launcherbrowsers: [‘Chrome‘],// Continuous Integration mode// if true, Karma captures browsers, runs the tests and exitssingleRun: false})}
/*** Created by weichunhe on 2015/11/10.*/var tests = [];for (var file in window.__karma__.files) { //这里的文件路径已经包含了 /base了if (window.__karma__.files.hasOwnProperty(file)) {if (/test1\.js$/.test(file)) {tests.push(file);}}}console.log(window.__karma__.files);console.log(tests);requirejs.config({// Karma serves files from ‘/base‘baseUrl: ‘/base/app/define‘,paths: {},shim: {},// ask Require.js to load these files (all our tests)deps: tests, //这里只包含需要测试的文件就可以了// start test run, once Require.js is donecallback: window.__karma__.start});
$injector = angular.injector([‘app‘]); //执行这步之后 注册的controller可以injector获取到require(‘app‘).register.controller(‘testCtrl‘, function (name) {console.log(‘controller‘, name);});$injector.invoke(function ($controller) {$controller(‘testCtrl‘, {name: ‘testCtrl‘});});
标签:
原文地址:http://www.cnblogs.com/vvch/p/4958972.html