1. 创建Ionic工程

ionic start projectname

cd projectname

2.安装karma插件

npm install karma karma-jasmine karma-phantomjs-launcher --save-dev --registry=https://registry.npm.taobao.org

3. 为了能使用karma命令

1) npm install karma karma-jasmine karma-phantomjs-launcher --save-dev --registry=https://registry.npm.taobao.org

2) npm install --registry=https://registry.npm.taobao.org

3) bower install angular-mocks --save-dev --registry=https://registry.npm.taobao.org

第三条出现如下错误

bower  ENOGIT git is not installed or not in the PATH

解决方法:

安装Git,并配置Git环境变量。 或者使用Git命令行工具进入projectname路径下执行

4. 创建测试文件夹

mkdir tests

cd tests

5. 初始化karma的配置文件

karma init my.conf.js

修改my.conf.js 文件

 files: [
'../www/build/polyfills.js',
'../www/build/vendor.js',
'../www/build/main.js',
'../www/build/0.js',
'**/*tests.js'
],
完整的配置
// Karma configuration
// Generated on Sun Jan 28 2018 20:59:56 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-adapter
frameworks: ['jasmine'], // list of files / patterns to load in the browser
files: [
'../www/build/polyfills.js',
'../www/build/vendor.js',
'../www/build/main.js',
'../www/build/0.js',
'**/*tests.js'
], // list of files / patterns to exclude
exclude: [
], // 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_INFO, // 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: ['Chrome'], // Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false, // Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}

 

6. 在tests目录下创建Controller文件夹

创建controllers.tests.js

describe("A spec ( with setup and tear-down)", function () {
var foo;
//在测试用例执行前加载
beforeEach(function () {
foo = 0;
foo += 1;
});
//在测试用例执行后加载
afterEach(function () {
foo = 0;
});
//测试用例
it("is just a function, so it can contain any code", function () {
//except 括号中是需要测试的函数,或是变量
//toEqual括号中是期望的函数防止或者变量
expect(foo).toEqual(1);
});
//测试用例--单个测试用例中,可以增加多个测试项
it("can have more than one expectation", function () {
expect(foo).toEqual(1);
expect(true).toEqual(true);
}); });

  

7. 运行测试

karma start tests/my.conf.js

出现错误:

Cannot load browser "Chrome": it is not registered! Perhaps you are missing some plugin?

解决方法:

npm install karma-chrome-launcher --registry=https://registry.npm.taobao.org

8. 返回测试结果

 

Ionic 使用karma进行单元测试的更多相关文章

  1. jasmine+karma 自动化单元测试

    测试的必须性 相信大家都知道测试的必要性,测试先行的概念.不过,写了这么多年的代码,除了之前用java的时候写过一些测试用例,还真的很少写过前端的测试用例,或者做一些自动化测试.感觉做单元测试还是很有 ...

  2. Karma:1. 集成 Karma 和 Jasmine 进行单元测试

    关于 Karma 会是一个系列,讨论在各种环境下,使用 Karma 进行单元测试. 本文讨论 karma 集成 Jasmine 进行单元测试. 初始化 NPM 实现初始化 NPM 包管理,创建 pac ...

  3. Karma +Jasmine+ require JS进行单元测试并生成测试报告、代码覆盖率报告

    1. 关于Karma Karma是一个基于Node.js的JavaScript测试执行过程管理工具(Test Runner). 该工具可用于测试所有主流Web浏览器,也可集成到CI(Continuou ...

  4. 在WebStorm中集成Karma+jasmine进行前端单元测试

    在WebStorm中集成Karma+jasmine进行前端单元测试 前言 好久没有写博了,主要还是太懒=.=,有点时间都去带娃.看书了,今天给大家分享一个原创的小东西,如果大家对TDD或者BDD有兴趣 ...

  5. Karma和Jasmine自动化单元测试

    从零开始nodejs系列文章,将介绍如何利Javascript做为服务端脚本,通过Nodejs框架web开发.Nodejs框架是基于V8的引擎,是目前速度最快的Javascript引擎.chrome浏 ...

  6. React单元测试——十八般兵器齐上阵,环境构建篇

    一个完整.优秀的项目往往离不开单元测试的环节,就 github 上的主流前端项目而言,基本都有相应的单元测试模块. 就 React 的项目来说,一套完整的单元测试能在在后续迭代更新中回归错误时候给与警 ...

  7. 初窥Javascript单元测试,附带掌握一门新技能的学习方式。

    之前没感觉要学啥单元测试,项目中测试都是手动测的,但也没觉的啥,但最近看文章和招聘上也多多少少有这方面的需求,于是网上搜索了一下,只找到了一些文章,但介绍的都不是很详细或者说比较复杂,满满的伤,虽然看 ...

  8. angularJS+requireJS并集成karma测试实践

    最近在为下一个项目做前端技术选型,Angular是必须要用的(BOSS指定,个人感觉也不错,开发效率会很高).由于需要加载的JS很多,所以打算看看angular和requirejs一起用会怎么样.在g ...

  9. 单元测试React

    React单元测试——十八般兵器齐上阵,环境构建篇 一个完整.优秀的项目往往离不开单元测试的环节,就 github 上的主流前端项目而言,基本都有相应的单元测试模块. 就 React 的项目来说,一套 ...

随机推荐

  1. Arduino 串口输出LM35温度

    #include "stdlib.h" float temp = 0.0; float maxtemp = 0.0; float mintemp =100.0; // the se ...

  2. session_id 生成原理

    PHPSESSID生成 生成规则是根据hash_func散列来生成的,相关的参数有: - 客户端IP - 当前时间(秒) - 当前时间(微妙) - PHP自带的随机数生产器 hash_func是php ...

  3. 搭建VUE项目

    1.换源由于npm源服务器在国内访问速度较慢,所以一般需要更换源服务器地址npm config set registry https://registry.npm.taobao.org也可以安装cnp ...

  4. python文件读写小结

    读文件 打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的): >>> f = open('test.txt', 'r') r表示是文本文件,rb是二进制文件 ...

  5. 【转】在python3.5安装tkinter

    链接:https://www.zhihu.com/question/42162071/answer/95441732 安装tkinter tkinter是由tcl和tk两个部分组成的,所以下载tkin ...

  6. Kesci: Keras 实现 LSTM——时间序列预测

    博主之前参与的一个科研项目是用 LSTM 结合 Attention 机制依据作物生长期内气象环境因素预测作物产量.本篇博客将介绍如何用 keras 深度学习的框架搭建 LSTM 模型对时间序列做预测. ...

  7. Buildroot Savedefconfig

    /********************************************************************************* * Buildroot Saved ...

  8. Python之路,第十一篇:Python入门与基础11

    python3  函数2 全局变量:一直存在 局部变量:函数执行时存在,执行完毕后销毁: lambda 表达式(又称匿名函数表达式) 作用: 创建一个匿名(无名)函数对象, 同 def 类似但不提供函 ...

  9. flask的安装

    1.查看已安装的Flask版本 在 python 的交互模式中 : 1. import flask 没报错:已经安装了Flask,可以继续查看版本 报错:没安装Flask 2. flask.__ver ...

  10. jqGrid 清空单元格的方法

    在工作中,使用到jqGrid的场景是比较多的. 今天在工作中遇到了一个jqGrid的问题,当将单元格的值设置成 '' 空字符串的时候,竟然不会修改成功. $("#mygrid"). ...