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. JavaWeb:脚本标识

    脚本标识 一.JSP表达式 1.介绍 用于向页面中输出信息 2.语法格式 <%= 表达式%> 3.注意 在"<%"和"="之间不允许有空格,但 ...

  2. Could not load driverClass com.mysql.jdbc.Driver错误

    在整合spring和mybatis的时候,在spring配置文件中已经加载了db.properties并配置了c3p0数据源 但在写了一个测试类测试是否取到了数据库的连接时,报错:Could not ...

  3. nwjs 解决手指可滑动问题

    package.json 中添加: "chromium-args": "--touch-events --enable-touch-drag-drop",

  4. 学习Java JDBC,看这篇就够了

    JDBC (Java DB Connection)---Java数据库连接 JDBC是一种可用于运行SQL语句的JAVA API(ApplicationProgramming Interface应用程 ...

  5. Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵

    Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...

  6. angular file change

    AngularJs: How to check for changes in file input fields? <input type="file" onchange=& ...

  7. 田螺便利店—win10专业版激活码

    win10专业版:VP4MG-CMX8Q-27THR-Y468R-HRVR7 开始——设置——更新和安全——激活——更改产品密钥 复制VP4MG-CMX8Q-27THR-Y468R-HRVR7即可激活 ...

  8. HURST 1116:选美大赛(LIS+路径输出)

    选美大赛 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 1099(318 users) Total Accepted: 349(252 ...

  9. 配置url防盗链、目录权限访问控制Directory、文件访问权限控制FilesMatch

    1.配置url防盗链: 编辑:/usr/local/apache2.4/conf/extra/httpd-vhosts 文件 写入: <Directory /var/www/222/>Se ...

  10. djjango cookie和session 的几种常用需求使用方法

    ------https://www.cnblogs.com/liuqingzheng/articles/8990027.html 需求情形一:正常设置cookie set_cookie(key,val ...