http://karma-runner.github.io/0.8/plus/RequireJS.html


karma 不是测试框架,只是一个运行测试框架的服务器

karma测试的原理是,将所有的文件都加载,然后执行对应的测试文件

使用到的前端自动化工具:
    grunt bower npm 等

安装依赖:
  1. "karma": "^0.13.15",
  2. "karma-chrome-launcher": "^0.2.1",
  3. "karma-jasmine": "^0.3.6",
  4. "karma-mocha": "^0.2.0", //与jasmine 二选一即可,建议使用jasmine,mocha需要其他的断言包
  5. "karma-requirejs": "^0.2.2",
初始化配置文件:
  1. karma init

配置说明:
    
  1. // Karma configuration
  2. // Generated on Tue Nov 10 2015 09:39:31 GMT+0800 (中国标准时间)
  3. module.exports = function (config) {
  4. config.set({
  5. // base path that will be used to resolve all patterns (eg. files, exclude)
  6. basePath: '',
  7. // frameworks to use
  8. // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
  9. frameworks: ['jasmine', 'requirejs'],
  10. // 配置要加载的文件
  11. //字符串形式的表示 通过<script>标签进行加载
  12. //对象形式的必须包含pattern、included属性,pattern进行匹配文件,included为true表示通过requirejs进行加载(后面会结束配置说明),否则通过script标签进行加载
  13. files: [
  14. 'app/bower_components/angular/angular.js',
  15. 'app/bower_components/angular-route/angular-route.js',
  16. 'app/bower_components/angular-mocks/angular-mocks.js',
  17. 'app/components/**/*.js',
  18. 'app/view*/**/*.js',
  19. {pattern: 'app/define/*.js', included: false},
  20. {pattern: 'app/test/*.js', included: false},
  21. 'test-main.js'
  22. ],
  23. // list of files to exclude
  24. exclude: [
  25. '**/*.swp'
  26. ],
  27. proxies: {
    //'/static': 'http://gstatic.com',
    '/log': 'http://localhost:3000' //避免跨域,测试异步请求时如果指定完整url就是跨域,会出错,如果只写路径会请求到karma服务器localhsot:9876/
    },

  28. // preprocess matching files before serving them to the browser
  29. // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
  30. preprocessors: {},
  31. // test results reporter to use
  32. // possible values: 'dots', 'progress'
  33. // available reporters: https://npmjs.org/browse/keyword/karma-reporter
  34. reporters: ['progress'],
  35. // web server port
  36. port: 9876,
  37. // enable / disable colors in the output (reporters and logs)
  38. colors: true,
  39. // level of logging
  40. // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
  41. logLevel: config.LOG_INFO,
  42. // enable / disable watching file and executing tests whenever any file changes
  43. autoWatch: true,
  44. // start these browsers
  45. // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
  46. browsers: ['Chrome'],
  47. // Continuous Integration mode
  48. // if true, Karma captures browsers, runs the tests and exits
  49. singleRun: false
  50. })
  51. }
requirejs配置
  1. /**
  2. * Created by weichunhe on 2015/11/10.
  3. */
  4. var tests = [];
  5. for (var file in window.__karma__.files) { //这里的文件路径已经包含了 /base了
  6. if (window.__karma__.files.hasOwnProperty(file)) {
  7. if (/test1\.js$/.test(file)) {
  8. tests.push(file);
  9. }
  10. }
  11. }
  12. console.log(window.__karma__.files);
  13. console.log(tests);
  14. requirejs.config({
  15. // Karma serves files from '/base'
  16. baseUrl: '/base/app/define',
  17. paths: {},
  18. shim: {},
  19. // ask Require.js to load these files (all our tests)
  20. deps: tests, //这里只包含需要测试的文件就可以了
  21. // start test run, once Require.js is done
  22. callback: window.__karma__.start
  23. });

启动:
cd node_modules/.bin/
karma.cmd start ../../karma.conf.js

karma:
    files: 根据正则表达式去遍历文件系统,将符合条件的文件在/base下面通过web提供服务
            将included指定为false时通过requirejs加载,如果指定为true 会出现  Uncaught Error: Mismatched anonymous define() module 这是因为在加载requirejs之前通过script标签进行加载的
    使用requirejs时:
        所有在deps里面的文件都会去加载执行,所以deps里面放的只是要测试的文件就可以了,要么define、要么直接写,不能require,这是异步了
    angular 的 $controllerProvider.reigster 在 angular启动之后注册的controller测试时通过angular-mock就加载不进来了

如果是所有controller都在angular bootstrap之前注册了,就可以使用 angular-mocks ,否则还是不要用了,太麻烦了;直接angular测试
  1. $injector = angular.injector(['app']); //执行这步之后 注册的controller可以injector获取到
  2. require('app').register.controller('testCtrl', function (name) {
  3. console.log('controller', name);
  4. });
  5. $injector.invoke(function ($controller) {
  6. $controller('testCtrl', {name: 'testCtrl'});
  7. });

karma+requirejs+angular 测试的更多相关文章

  1. angular测试-Karma + Jasmine配置

    首先讲一下大致的流程: 需要node环境,首先先要安装node,node不会?请自行搜索.版本>0.8 安装node完成之后先要测试下npm是否测试通过,如下图所示 首先看下目录结构 目录为:F ...

  2. karma和jasmine的测试(包括angular测试)

    本篇博客主要就是针对现在日新月异的技术和快速开发,测试被很多人忽略,其实在开发中如何保证代码的质量以及逻辑的完整性,测试显得十分重要,本文就是负责karma+jasmine来测试. 1.搭建测试的环境 ...

  3. 浅谈HTML5单页面架构(一)——requirejs + angular + angular-route

    心血来潮,打算结合实际开发的经验,浅谈一下HTML5单页面App或网页的架构. 众所周知,现在移动Webapp越来越多,例如天猫.京东.国美这些都是很好的例子.而在Webapp中,又要数单页面架构体验 ...

  4. H5单页面架构:requirejs + angular + angular-route

    说到项目架构,往往要考虑很多方面: 方便.例如使用jquery,必然比没有使用jquery方便很多,所以大部分网站都接入类似的库: 性能优化.包括加载速度.渲染效率: 代码管理.大型项目需要考虑代码的 ...

  5. AngularJS进阶(二十五)requirejs + angular + angular-route 浅谈HTML5单页面架构

    requirejs + angular + angular-route 浅谈HTML5单页面架构 众所周知,现在移动Webapp越来越多,例如天猫.京东.国美这些都是很好的例子.而在Webapp中,又 ...

  6. Angular测试遇到的小坑

    Angular测试遇到的小坑 Error: Expected to be running in 'ProxyZone', but it was not found 检查doneFn的写法是否正确,位置 ...

  7. 用Karma和Jasmine测试Angular应用

    TEST: Before you've written any of the code, you know how you want it to behave. You have a specific ...

  8. karma mocha angular angular-mock 测试

    describe('工具方法测试', function () { var utilsModule; beforeEach(function () { module('Admin'); // modul ...

  9. karma+requirejs

    下面的介绍以karma能正常运行为前提,看karma系列文章:http://www.cnblogs.com/laixiangran/tag/Karma/ 目录结构 步骤 安装 npm install ...

随机推荐

  1. html中布局,让下一个子元素占据剩余的高度

    ---------------------------------------------------------------------- 原因是: height:100% 引起的, 这句话的意思是 ...

  2. swift学习笔记(四)关于类的继承

    在swift中,继承是区分类与其它对象的基本特征 继承后的子类能够重写父类的方法,包含类方法和实例方法,属性和附属脚本(subscript) 在继承过程中,构造器方法init()是不被继承的,须要显示 ...

  3. MariaDB -- 数据类型

    Mariadb 的数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的 常用的数据类型 整数:int, bit 小数:d ...

  4. 记录一次node中台转发表单上传文件到后台过程

    首发掘金 记录一次node中台转发表单上传文件到后台过程 本篇跟掘金为同一个作者leung   公司几个项目都是三层架构模式即前台,中台(中间层),后台.前台微信端公众号使用vue框架,后台管理前端使 ...

  5. Spring MVC 数据验证——validate编码方式

    1.导入jar包 validation-api-1.0.0.GA.jar这是比較关键的一个jar包,主要用于解析注解@Valid. hibernate-validator-4.3.2.Final.ja ...

  6. Mac OS X 10.10, Eclipse+ADT真机调试代码时,Device Chooser中不显示真机的解决方式

    Mac OS X 10.10的环境下.Eclipse+ADT,进行真机调试时,会出现一个问题. Device Chooser对话框里不显示真机设备,仅仅有又一次插拔数据线才干够. 经过測试.有两个暂时 ...

  7. 在Android程序中使用已有的SQLite数据库

    已经将这篇文章迁移至 Code问答,你也能够到这里查看这篇文章,请多多关注我的新技术博客CodeWenDa.com 在中文搜索中,没有找到一篇比較好的关于怎样在Android应用中使用自己事先创建好的 ...

  8. JPA測试实例

    依赖架包 实体 import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.G ...

  9. 深度理解apache 重写模块rewrite_mod,重写不再犯错

    1.RewriteRule ^(com\/.*)$ index.php?do=$1 问:上面的规则匹配表达式 "^(.*)$" 匹配的内容是什么 答:匹配内容是URI站点目录:/d ...

  10. SpringMVC_1

    //@SessionAttributes(value={"user"},types={String.class}) @Controller public class SpringM ...