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. windows下计算文件的md和sha值

    在windows下可以使用FCIV命令行工具计算文件的md5和sha值,具体例子如下: FCIV -md5 -sha1 path\filename.ext 例如: FCIV-md5-sha1 c:\w ...

  2. [CSS3] CSS :target Selector

    The :target selector allows us to interact with a fragment identifier, or hash, in our URL from CSS. ...

  3. 编程算法 - 连续子数组的最大和 代码(C)

    连续子数组的最大和 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一个整型数组, 数组里有正数也有负数. 数组中一个或连续的多个整数组成一 ...

  4. [转] Linux文件系统之hard link&symbol link

    这个图很清楚的表示出硬链接和软链接的方式. 1.硬链接: 基本定义:硬链接是有着相同inode号的仅文件名不同的文件(该文件名包含路径信息). 理解:如图,hard link和原始file通过同一个i ...

  5. B/S系统间跨域单点登录设计思路

    基于B/S系统间单点登录 此处说的单点登录的概念,即不同系统公用一个登录界面.一处系统通过登录验证,在接入的各系统均为登录状态.一般有两种情景: 1)  一级域名相同 例如:tieba.baidu.c ...

  6. [JS] 如何清空file input框 [整理]

    测试环境:OS --> winXPBrowsers --> IE6+, FF 3.0.11, FF 3.5, Opera 9.64, Opera 10 beta 2, Safari 4, ...

  7. 分页技术之PageDataSource类

    之前给大家介绍了分页技术之Gridview控件,今天给大家介绍另外一种分页技术,采用PageDataSource类 + Repeater控件来实现. 前台只需要拖出一个Repeater控件来绑定要显示 ...

  8. sql语句分页代码

    SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO alter proc sp_SelectInfomationByKeyWord--创建一个存储过程 - ...

  9. 【转】UITextView 修改键盘 的return按钮

    原文:http://www.apkbus.com/blog-107838-45740.html 1 #import <UIKit/UIKit.h>2 3 @interface TextVi ...

  10. UIScrollView 之图片缩放

    UIScrollView 之图片缩放 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对其内容进行缩放处理 也就是说,要完成缩放功能的话,只 ...