AngularJS测试二 jasmine测试路由 控制器 过滤器 事件 服务
测试应用
.config(function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'HomeController'})
.when('/login', {
templateUrl: 'views/login.html',
controller: 'LoginController'})
.otherwise({redirectTo '/'});
})
(2)建立一个模拟的后端来处理XHR,获取模板代码。
(3)设置一个地址,运行一个$digest生命周期
// 在测试中模拟模块
beforeEach(module('myApp'));
var location, route, rootScope;
beforeEach(inject(
function(_$location_, _$route_, _$rootScope_) {
location = _$location_;
route = _$route_;
rootScope = _$rootScope_;
}));
describe('index route', function() {
beforeEach(inject(
function($httpBackend) {
$httpBackend.expectGET('views/home.html')
.respond(200, 'main HTML');
}));
// 我们的测试代码放在这里
});
});
为了用单元测试来测试路由,我们需要模拟路由在生产中的运作。路由借助于digest生命周期来运行,当location被设置以后,它使用一个
digest循环周期来处理路由,改变页面内容,然后结束本次路由。了解了这些之后,我们就需要解释测试中路径的变更。
function() {
location.path('/');
rootScope.$digest(); // 调用digest循环
expect(route.current.controller)
.toBe('HomeController')
});
it('should redirect to the index path on non-existent
route', function() {
location.path('/definitely/not/a/_route');
rootScope.$digest();
expect(route.current.controller)
.toBe('HomeController')
建立了测试来模拟模块;
用一个已知的作用域实例来存储控制器的一个实例;
基于作用域来测试我们的预期。
// 模拟myApp模块
beforeEach(module('myApp'));
describe('FrameController', function() {
// 局部变量
var FrameController, scope;
beforeEach(inject(
function($controller, $rootScope) {
// 创建子作用域
scope = $rootScope.$new();
// 创建FrameController的新实例
FrameController = $controller('FrameController',
{ $scope: scope });
}));
// 我们的测试代码放在这里
});
.controller('FrameController',
function($scope, $timeout) {
$scope.time = {
today: new Date()
};
$scope.user = {
timezone: 'US/Pacific'
}
var updateClock = function() {
$scope.time.today = new Date();
};
var tick = function() {
$timeout(function() {
$scope.$apply(updateClock);
tick();
}, 1000);
}
tick();
expect(scope.time.today).toBeDefined();
});
it('should have a user set', function() {
expect(scope.user).toBeDefined();
describe('Unit: Filter tests', function() {
var filter;
// 在测试中模拟我们的引用
beforeEach(module('myApp'));
beforeEach(inject(function($filter) {
filter = $filter;
}));
});
有了对控制器的访问,在过滤器的输出上设置预期就是很容易的事了。
it('should give us two decimal points',
function() {
expect(filter('number')(123, 2)).toEqual('123.00');
});
var $httpBackend,
location,
route,
rootScope;
beforeEach(module('myApp'));
beforeEach(inject(
function(_$rootScope_, _$route_, _$httpBackend_, _$location_){
location = _$location_;
rootScope = _$rootScope_;
route = _$route_;
$httpBackend = _$httpBackend_;
}));
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
});
// 我们的测试代码放在这里
});
现在,可以建立测试来反映导航到应用不同部分时的预期。
it('loads the home template at /', function() {
$httpBackend.expectGET('templates/home.html')
.respond(200);
location.path('/');
rootScope.$digest(); // 调用digest循环
$httpBackend.flush();
});
it('loads the dashboard template at /dashboard', function() {
$httpBackend.expectGET('templates/dashboard.html')
.respond(200);
location.path('/dashboard');
rootScope.$digest(); // 调用digest循环
$httpBackend.flush();
});
使用Jasmine的辅助方法spnOn()可以非常容易地建立事件测试。设想我们在测试一个控制器,它触发了一个$emit函数。基于这个函数,我们可以建立一个预期:事件被触发了,并且用我们所感兴趣的任意参数调用了。
var scope;
beforeEach(angular.mock.module('myApp'));
beforeEach(angular.mock.inject(function($rootScope) {
scope = $rootScope.$new();
});
});
测试建好之后,就可以简单地在作用域上为$emit或者$broadcase事件设置一个spyOn()调用了。
// ...
});
it('should have emit called', function() {
spyOn(scope, "$emit");
scope.closePanel(); // 示例
// 或者任意可能导致emit被调用的事件
expect(scope.$emit)
.toHaveBeenCalledWith("panel:closed",
panel.id);
});
我们也可以测试事件:设置一个事件触发后调用的$on()监听器。要执行$broadcast方法,
可以简单地在作用域上调用$broadcast,并且对事件将会导致的作用域变化建立一个预期。
// ...
it('should set the panel to closed state',
function() {
scope.$broadcast("panel:closed", 1);
expect(scope.panel.state).toEqual("closed");
});
AngularJS测试二 jasmine测试路由 控制器 过滤器 事件 服务的更多相关文章
- 【转】学习使用Jmeter做压力测试(二)--压力测试的实施
JMeter测试步骤: 1.建立测试计划 2.添加线程组 3.添加HTTP请求 4.增加监听器 5.执行测试计划 6.根据JMeter提供的报告分析结果 一.目标 测试访问目标服务器网站首页的每秒查询 ...
- angularJS(二):作用域$scope、控制器、过滤器
app.controller创建控制器 一.作用域 Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. ...
- 用Karma和Jasmine测试Angular应用
TEST: Before you've written any of the code, you know how you want it to behave. You have a specific ...
- Android APP压力测试(二)之Monkey信息自动收集脚本
Android APP压力测试(二) 之Monkey信息自动收集脚本 前言: 上一篇Monkey介绍基本搬抄官方介绍,主要是为了自己查阅方便.本文重点介绍我在进行Monkey时如何自动收集相关信息 ...
- angular测试-Karma + Jasmine配置
首先讲一下大致的流程: 需要node环境,首先先要安装node,node不会?请自行搜索.版本>0.8 安装node完成之后先要测试下npm是否测试通过,如下图所示 首先看下目录结构 目录为:F ...
- 总结Selenium自动化测试方法(二)测试环境搭建
(接上期内容) 二.测试环境搭建 1.安装python 现在python3.0比python2.0多了一些改进的功能(详见http://zhidao.baidu.com/link?url=3sT1g7 ...
- 大数据项目测试<二>项目的测试工作
大数据的测试工作: 1.模块的单独测试 2.模块间的联调测试 3.系统的性能测试:内存泄露.磁盘占用.计算效率 4.数据验证(核心) 下面对各个模块的测试工作进行单独讲解. 0. 功能测试 1. 性能 ...
- 数据驱动测试二:使用TestNG和CSV文件进行数据驱动
转载:https://blog.csdn.net/heart_1014/article/details/52013173 使用@DataProvider注解定义当前方法中的返回对象CSV文件(存放测试 ...
- stm32+lwip(二):UDP测试
我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...
随机推荐
- Nginx和Apache共存环境下apache获得真实IP
自从Nginx出现以后,我们都喜欢让 Nginx 跑在前方处理静态文件,然后通过 proxy 把动态请求过滤给 apache.这么有个问题,跑在后方 apache 上的应用获取到的IP都是Nginx所 ...
- Deep Learning 学习随记(六)Linear Decoder 线性解码
线性解码器(Linear Decoder) 前面第一章提到稀疏自编码器(http://www.cnblogs.com/bzjia-blog/p/SparseAutoencoder.html)的三层网络 ...
- undefined与null的区别(待修整)
没有实体的对象称为空对象.只用对象的引用,而不存在引用的实体对象 就叫做空对象 在常见的强类型语言中,通常有一个表示"空"的值,比如NULL.但是在Javascript中,空(或者 ...
- Windows7 IIS7 无法启动计算机上的服务W3SVC如何修复
错误提示 启动iis7管理服务器提示:无法启动计算机上的服务W3SVC 启动Windows Process Activation Service服务,报错:6801 指定资源管理器中的事务支持未启动或 ...
- ie8 background css没有显示?——都是空格惹的祸
ie8 background css没有显示?——都是空格惹的祸
- 谨慎使用php的strtotime()函数
我们在日常业务中,针对业务量,经常会采用对数据库按时间做横向分表,分表后的查询往往会涉及到时间问题.例如,我们想查询某个用户距离当前时间1个月的订单情况,在这个时候,我们有些会用到strtotime( ...
- 使用Raphael 画图(一) 基本图形 (javascript)
Raphael是什么? Raphael 是一个用于在网页中绘制矢量图形的 Javascript 库.它使用 SVG W3C 推荐标准和 VML 作为创建图形的基础,你可以通过 JavaScript 操 ...
- 【转】HTML5 LocalStorage 本地存储
原文见:http://www.cnblogs.com/xiaowei0705/archive/2011/04/19/2021372.html 说到本地存储,这玩意真是历尽千辛万苦才走到HTML5这一步 ...
- django-orm-standalone
django-orm-standalone script via:https://github.com/masnun/django-orm-standalone/ # Django specific ...
- Title of live Writer
Test From Windows Live Writer **markdown bold**