1:工程目录结构
y@y:karma-t01$ tree -L 3
.
├── client
│   ├── app
│   │   └── user
│   ├── bower_components
│   │   ├── angular
│   │   ├── angular-mocks
│   │   └── angular-resource
│   └── bower.json
├── karma.conf.js
└── readme

7 directories, 3 files
项目说明:使用bower进行js包管理,使用Karma及Jasmine完成基本测试.
使用bower在线安装工程所依赖的包:
y@y:client$ bower install angular
y@y:client$ bower install angular-resource
y@y:client$ bower install angular-mocks
user目录结构:
y@y:app$ tree -L 1 user/
user/
├── user.js
├── users.json
└── user.test.js

0 directories, 3 files
2:user.js
/**
* Created by y on 15-11-24.
*/
'use strict'; var app = angular.module('Application', ['ngResource']); app.factory('UserFactory', function($resource){
return $resource('users.json',{},{
query:{method:'GET',isArray:true}
});
}); app.controller('MainCtrl', function($scope,UserFactory) {
$scope.title = 'Hello AngularJS In Action!';
$scope.users = UserFactory.query();
});
3:user.test.js
/**
* Created by y on 15-11-24.
*/
'use strict'; describe('MainCtrl',function(){
var scope,httpBackend; beforeEach(angular.mock.module('Application')); beforeEach(angular.mock.inject(function($rootScope,$controller,_$httpBackend_){ httpBackend = _$httpBackend_;
httpBackend.when('GET','users.json')
.respond([
{
name:'张三', age:25
},
{
name:'李四', age:24
},
{
name:'王五', age:27
}
]); scope = $rootScope.$new(); $controller('MainCtrl',{$scope:scope});
})); //test begin
it('should have variable title="Hello AngularJS In Action!"',function(){
expect(scope.text).toBe('Hello AngularJS In Action!');
}); //test begin
it('should fetch list of users', function(){
httpBackend.flush(); expect(scope.users.length).toBe(3);
expect(scope.users[0].name).toBe('张三');
}); });
4:users.json
[
{
name:'张三', age:25
},
{
name:'李四', age:24
},
{
name:'王五', age:27
}
]
 
5:进行Karma文件配置,Karma默认使用Jasmine作为测试框架.
切换到你想要放置配置文件的目录,然后在终端中输入下面的命令来创建配置文件:
y@y:karma-t01$ karma init karma.conf.js
配置信息如下:主要进行files节点配置
// Karma configuration
// Generated on Tue Nov 24 2015 23:12:58 GMT+0800 (CST) 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: [
'client/bower_components/angular/angular.js',
'client/bower_components/angular-mocks/angular-mocks.js',
'client/bower_components/angular-resource/angular-resource.js',
'client/app/**/*.js'
],
// list of files 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
});
};
6:在控制台输入命令进行测试
y@y:karma-t01$ karma start karma.conf.js 
INFO [karma]: Karma v0.12.32 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 39.0.2171 (Linux)]: Connected on socket CO5e5TAX7Pv9PzqGAAAA with id 34588683
Chrome 39.0.2171 (Linux): Executed 2 of 2 SUCCESS (0.06 secs / 0.048 secs)

 
提示执行2个测试,成功两个.
 
7:参考链接

基于Karma和Jasmine的AngularJS测试的更多相关文章

  1. 基于karma和jasmine的Angularjs 单元测试

    Angularjs 基于karma和jasmine的单元测试 目录: 1. 单元测试的配置 2. 实例文件目录解释 3. 测试controller     3.1 测试controller中变量值是否 ...

  2. karma、jasmine做angularjs单元测试

    引用文:karma.jasmine做angularjs单元测试 karma和jasmine介绍 <1>技术介绍 karma karma是Testacular的新名字 karma是用来自动化 ...

  3. Angularjs 基于karma和jasmine的单元测试

    目录: 1. 单元测试的配置 2. 实例文件目录解释 3. 测试controller     3.1 测试controller中变量值是否正确     3.2 模拟http请求返回值,测试$http服 ...

  4. 利用Angularjs测试引擎Karma进行自动化单元测试

    Karma是Google用于angularjs框架单元测试的js引擎(javascript test runner ), angular1 和angular2项目源码的单元测试都是基于karma和ja ...

  5. angularJS测试一 Karma Jasmine Mock

    AngularJS测试 一 测试工具 1.NodeJS领域:Jasmine做单元测试,Karma自动化完成单元测试,Grunt启动Karma统一项目管理,Yeoman最后封装成一个项目原型模板,npm ...

  6. Karma和Jasmine自动化单元测试——本质上还是在要开一个浏览器来做测试

    1. Karma的介绍 Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma.Karma是一个让人感到非常神秘的 ...

  7. AngularJS测试框架 karma备忘

    AngularJS测试框架karma安装 安装karma $ --save-dev 安装karma组件 $ npm install karma-jasmine karma-chrome-launche ...

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

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

  9. Karma和Jasmine 自动化单元测试环境搭建

    最近初学AngularJS ,看到的一些教程中经常有人推荐使用Karma+Jasmine来进行单元测试.自己之前也对Jasmine有些了解,jasmine也是一个不错的测试框架. 1. karma介绍 ...

随机推荐

  1. ie8下$(document).on('mouseover mouseout','ul li',function(){})的bug

    $(document).on('mouseover mouseout','ul li',function(){ if (event.type == 'mouseover') {           c ...

  2. Android中关于Task的一些认识

    Android中Task是一个逻辑上的概念,简单地说,就是一个栈里面顺序存储着的多个Activity.这些Activity能够是来自同一个App,也能够是来自不同的Apps. Task的创建 比方之前 ...

  3. 如何理解oracle 11g scan ip

    如何理解oracle 11g scan ip   在11.2之前,client链接数据库的时候要用vip,假如你的cluster有4个节点,那么客户端的tnsnames.ora中就对应有四个主机vip ...

  4. ExtJS4.2学习(6)——基础知识之proxy篇

    本次讨论下数据代理,其实个人第一次听到这个短语的时候,并不是特别的适应,在英语中的含义是proxy,其实如若大家也觉得不适应的话,就直接称呼proxy吧. 在ExtJS中,proxy是进行数据读写的主 ...

  5. Cocos2d-x 3.0 实例学习教程 前沿

    前一段时间学过cocos2d-x  2.x ,后来去做了一些别的项目.近期又想开发自己的游戏了,但是cocos2d-x 已经升级到3.0 ,好多API都变了.所以决定再把cocos2d-x学一遍,一是 ...

  6. linux查看cpu温度

      分类: linux系统 一.安装  sudo apt-get install lm-sensors   二.查看 linux@cdyemail:~$ sensors k10temp-pci-00c ...

  7. 解决下载android sdk慢的问题

    修改host文件 203.208.46.146 dl.google.com 203.208.46.146 dl-ssl.google.com 强制不使用https访问 在sdk manager里选择t ...

  8. python环境准备

    一.环境准备. 1.安装python3.5.2(勾选环境变量),python2.7.12 2.设置环境变量 (要求命令行输入python,进入python2命令行,打python3时,进入python ...

  9. gir配置

    Git配置 1. 用户信息 你个人的用户名称和电子邮件地址,用户名可随意修改,git 用于记录是谁提交了更新,以及更新人的联系方式. $ git config --global user.name & ...

  10. JavaScript设计模式之观察者模式(学习笔记)

    设计模式(Design Pattern)对于软件开发来说其重要性不言而喻,代码可复用.可维护.可扩展一直都是软件工程中的追求!对于我一个学javascript的人来说,理解设计模式似乎有些困难,对仅切 ...