karma+angular
下面的介绍以karma能正常运行为前提,看karma系列文章:http://www.cnblogs.com/laixiangran/tag/Karma/
目录结构

步骤
安装
npm install angular --save-dev
npm install angular-mocks --save-dev //专门用来进行单元测试的模块
karma.conf.js
/***
* Created by laixiangran on 2015/12/22.
* karma单元测试配置文件
*/ module.exports = function(config) { config.set({ /***
* 基础路径,用在files,exclude属性上
*/
basePath: "", /**
* 测试框架
* 可用的框架:https://npmjs.org/browse/keyword/karma-adapter
*/
frameworks: ["jasmine"], /**
* 需要加载到浏览器的文件列表
*/
files: [
"../node_modules/angular/angular.js",
"../node_modules/angular-mocks/angular-mocks.js",
"karmaTest/test.js",
"karmaTest/test.spec.js"
]
});
};
test.js
/**
* Created by laixiangran on 2015/12/20.
*/ var app = angular.module("myApp", []); app.controller("myCtrl", ["$scope", function ($scope) { var vm = $scope.vm = {
htmlSource: "",
showErrorType: 1,
showDynamicElement: true
}; $scope.testTxt = "hello unit test!"; $scope.setTxt = function (txt) {
$scope.testTxt = txt;
}; $scope.getTxt = function () {
return $scope.testTxt;
}; $scope.removeTxt = function () {
delete($scope.testTxt);
}; $scope.chooseTxt = function (val) {
return val == "t" ? "hello unit test!" : "hello world!";
}; }]);
test.spec.js
/**
* Created by laixiangran on 2015/12/20.
*/
describe("myCtrl测试", function() {
var scope = null;
var testCtrl = null; // module是angular.mock.module方法,用来配置inject方法注入的模块信息,参数可以是字符串、函数、对象
beforeEach(module("myApp"));
// inject是angular.mock.inject方法,用来注入module配置好的ng模块,方便在it的测试函数里调用
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
//初始化myCtrl
testCtrl = $controller("myCtrl", {$scope:scope});
})); it("validateCtrl必须定义", inject(function($controller) {
expect(testCtrl).toBeDefined();
})); it("scope.testTxt = 'hello unit test!'",function() {
expect(scope.testTxt).toBe("hello unit test!");
}); it("scope.setTxt('hello world!'),scope.testTxt = 'hello world!'",function() {
scope.setTxt("hello world!");
expect(scope.testTxt).toBe("hello world!");
}); it("scope.chooseTxt('t')必须返回'hello unit test!'",function() {
expect(scope.chooseTxt("t")).toBe("hello unit test!");
}); });
karma+angular的更多相关文章
- 【17】进大厂必须掌握的面试题-50个Angular面试
我们整理了一份主要的Angular面试问题清单,分为三部分: 角度面试问题–初学者水平 角度面试问题–中级 角度面试问题–高级 初学者水平–面试问题 1.区分Angular和AngularJS. 特征 ...
- Angular+Grunt+Bower+Karma+Protractor (Atom)
1. 配置bower 1.安装bower npm install -g bower 2.创建.bowerrc文件 { "directory": "src/bower&qu ...
- angular测试-Karma + Jasmine配置
首先讲一下大致的流程: 需要node环境,首先先要安装node,node不会?请自行搜索.版本>0.8 安装node完成之后先要测试下npm是否测试通过,如下图所示 首先看下目录结构 目录为:F ...
- 用Karma和Jasmine测试Angular应用
TEST: Before you've written any of the code, you know how you want it to behave. You have a specific ...
- karma和jasmine的测试(包括angular测试)
本篇博客主要就是针对现在日新月异的技术和快速开发,测试被很多人忽略,其实在开发中如何保证代码的质量以及逻辑的完整性,测试显得十分重要,本文就是负责karma+jasmine来测试. 1.搭建测试的环境 ...
- karma+requirejs+angular 测试
http://karma-runner.github.io/0.8/plus/RequireJS.html karma 不是测试框架,只是一个运行测试框架的服务器 karma测试的原理是,将所有的文件 ...
- [Unit Testing] Configure the Angular CLI to use the Karma Mocha test reporter
Every Angular CLI generated project comes already with Karmapreinstalled as well a couple of executa ...
- 与karma、angular的第一次亲密接触
首先要了解什么是karma,karma干嘛用的,它的好朋友jasmine又是啥?这些文章可以帮助你: karma干嘛的? angular与karma1 angular与karma2 看了以上几篇文章之 ...
- karma mocha angular angular-mock 测试
describe('工具方法测试', function () { var utilsModule; beforeEach(function () { module('Admin'); // modul ...
随机推荐
- python递归小疑惑
代码如下: def crawlAndGet(keyword, n): if n>0: jsondata = os.system('scrapy crawl beijing -a store_di ...
- Tomcat优化总结
一.内存溢出问题 Linux设置启动脚本 [root@LAMP ~]# vi /usr/local/tomcat/bin/catalina.sh #__________________________ ...
- JAVA中生成Excel方法
java 操作 Excel 最常用的就是JXL(java excel api)和POI,今先看下JXL吧.首先可以到 http://www.andykhan.com/jexcelapi/downloa ...
- Bad apple for CSharp
前言:记得10年的时候我还在上学,有一天逛csdn看到了字符版的badapple,感觉这东西好NB啊,然后就下载了一份,最近整理博客就把他整理博客,原作者是谁真心不知道,这是在果壳看到的. Bad A ...
- Python3基础 大于一个数的同时小于一个数
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- ubuntu下ssh登陆阿里云服务器(ubuntu系统)中文乱码问题
研究了几天终于解决了... 原文地址: http://blog.csdn.net/a__yes/article/details/50489456 问题描述: 阿里云的服务器ubuntu系统,wind ...
- 【原创】VB6.0应用程序安装包的生成(Setup Factory 9.0制作安装包的方法)
VB6.0应用程序安装包的生成,利用其自带的打包工具生成的安装程序很简陋,一点不美观:如果想让自己的应用程序安装的时候显得高大上一点,本教程提供使用Setup Factory 9.0制作安装包的方法. ...
- 【转】Kali Linux 新手折腾笔记
原作者:http://defcon.cn/1618.html 最近在折腾Kali Linux 顺便做一简单整理,至于安装就不再多扯了,估计会出现的问题上一篇文章<VMware虚拟机安装Kali ...
- HTML笔记(四) 框架
通过框架,可以在一个窗口显示多个页面.而所谓的框架,就是指每一份HTML文档. 框架结构标签<frameset> 定义如何将窗口分割为框架. frameset定义了一系列的行列. rows ...
- iOS - 3DTouch 3D 触摸
1.3DTouch 简介 3DTouch 是 iOS9 + 系统下,在 iPhone6s(iPhone6s Plus)+ 手机上才能够使用的功能. 1.1 3DTouch 基本类型 1.主屏幕快速选项 ...