下面的介绍以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!");
}); });

好文推荐:http://www.ngnice.com/posts/dc4b032b537ae0

karma+angular的更多相关文章

  1. 【17】进大厂必须掌握的面试题-50个Angular面试

    我们整理了一份主要的Angular面试问题清单,分为三部分: 角度面试问题–初学者水平 角度面试问题–中级 角度面试问题–高级 初学者水平–面试问题 1.区分Angular和AngularJS. 特征 ...

  2. Angular+Grunt+Bower+Karma+Protractor (Atom)

    1. 配置bower 1.安装bower npm install -g bower 2.创建.bowerrc文件 { "directory": "src/bower&qu ...

  3. angular测试-Karma + Jasmine配置

    首先讲一下大致的流程: 需要node环境,首先先要安装node,node不会?请自行搜索.版本>0.8 安装node完成之后先要测试下npm是否测试通过,如下图所示 首先看下目录结构 目录为:F ...

  4. 用Karma和Jasmine测试Angular应用

    TEST: Before you've written any of the code, you know how you want it to behave. You have a specific ...

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

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

  6. karma+requirejs+angular 测试

    http://karma-runner.github.io/0.8/plus/RequireJS.html karma 不是测试框架,只是一个运行测试框架的服务器 karma测试的原理是,将所有的文件 ...

  7. [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 ...

  8. 与karma、angular的第一次亲密接触

    首先要了解什么是karma,karma干嘛用的,它的好朋友jasmine又是啥?这些文章可以帮助你: karma干嘛的? angular与karma1 angular与karma2 看了以上几篇文章之 ...

  9. karma mocha angular angular-mock 测试

    describe('工具方法测试', function () { var utilsModule; beforeEach(function () { module('Admin'); // modul ...

随机推荐

  1. 制作透明色:《CSS3 RGBA》与Opacity样式用法

    前面我们一起探讨了一下CSS3 Gradient(css3 渐变),今天我们一起来探讨一下CSS3中的RGBA.RGB对于大家来说一点不陌生,他就是红色R+绿色G+蓝色B,那现在我们所说的RGBA又是 ...

  2. EF 存储过程

    今天我们利用EF执行sql语句的方式来执行存储过程,并得到OutPut的值. 首先新建存储过程: Create PROCEDURE proc_testEF   (     @id int,     @ ...

  3. Android 环信的使用

    1.导入包 http://docs.easemob.com/doku.php?id=start:200androidcleintintegration:10androidsdkimport 在清单文件 ...

  4. SqlSever基础 datepart 获取一个日期的年份

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  5. SqlSever基础 top 后面的n 由一个简单的计算式子计算得出

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  6. 如何用linux远程登录windows计算机

    大家可能试过用windows远程登录另一个windows pc机,今天大家将会学到如何用 linux远程登录你的windows系统. 首先大家要做到得救是将自己linux和windows操作机的IP地 ...

  7. SQL数据库约束行为---防止数据乱填(即数据规范化)

    防止乱填:一.Check约束.按照某种规则对数据进行检查.操作:在表的设计界面中,右击相应的列,选择“CHECK约束”在弹出的对话框中,设置约束的名称和表达式. 代码实现: create table ...

  8. 【leetcode❤python】66. Plus One

    class Solution:      # @param digits, a list of integer digits      # @return a list of integer digi ...

  9. CSS3弹力球

    如下代码实现方块在限定区域内不停弹跳. <!DOCTYPE html> <html> <head> <style> div { width:100px; ...

  10. 图片轮播的JS写法,通用涉及多个轮播

    本代码是借鉴大神的代码分析理解后,自己改写的!有不足指出希望给为大神指点. 核心只有一个JS,里面包含了css样式. 展示效果图: