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. MVC 文件上传

    项目需要,做一个图片上传的功能,本来是很简单,但是需要同时上传多个文件,并分条带一些额外的信息,听上去很复杂,通过下面图就可以一目了然: 网上找过一些方法,但多为不支持图片与其他信息关联,或者分两次上 ...

  2. iOS10相机等崩溃

    当使用iOS10使用相机时会出现崩溃 This app has crashed because it attempted to access privacy-sensitive data withou ...

  3. maven建module子模块

    在父工程中,点击new ->other  ->maven  -> maven module, 按照提示直到完成. module 可以是普通的工程也可以是web工程. 遇到的问题: 新 ...

  4. backgroundworker组件的使用

    本文转载:http://www.cnblogs.com/inforasc/archive/2009/10/12/1582110.html BackgroundWorker 组件用来执行诸如数据库事务. ...

  5. What is therelationship between @EJB and ejb-ref/ejb-local-ref?

    http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html What is therelationship between @EJB and ejb-ref/ ...

  6. [PWA] 0. Introduce to Offline First

    Why offline first? Imagin you are visiting a website, it is fine if wifi connection is good. It migh ...

  7. [ArcGIS所需的补丁]ArcGIS 10.2.2 for Desktop联系Oracle(2014年10上个月发布)数据库崩溃

    环境的叙述性说明: ArcGIS 10.2.2 for Desktop.Oracle12.1.0.2 RAC 这仅仅是用户当时环境描写叙述.可是导致该问题的解决办法还包含很多其它的环境! 问题描写叙述 ...

  8. PHP数据结构预热:PHP的迭代器(转)

    迭代器有时又称光标(cursor)是程式设计的软件设计模式,可在容器物件(container,例如list或vector)上遍访的接口,设计人员无需关心容器物件的内容. 各种语言实作Iterator的 ...

  9. instancetype vs id for Objective-C

    instancetype: 使用 instancetype 编译器和IDE 会做类型检查,而id不会做完整的类型检查. A method with a related result type can ...

  10. ACM vim配置

    ACM现场赛时用的,比较简短,但是主要的功能都有了. 直接打开终端输入gedit ~/.vimrc 把下面的东西复制到里面就行了. filetype plugin indent on colo eve ...