Jasmine测试ng Promises - Provide and Spy
jasmine提供了很多些很实用的处理Promises的方法,首先我们来考虑下面的这个例子:
angular.module("myApp.store").controller("StoresCtrl", function($scope, StoreService, Contact) {
StoreService.listStores().then(function(branches) {
Contact.retrieveContactInfo().then(function(userInfo) {
//more code here crossing user and stores data
});
});
});
下面让我们来尝试如何用angular提供的$provide创建一个依赖的实现,以及利用jasmine帮助我们fake方法的返回值:
代码如下,有详细注释帮助你去理解这段代码:
describe("Store Controller", function() {
var $controller, Contact, StoreService, createController, scope;
beforeEach(function() {
module('myApp.store');
// Provide will help us create fake implementations for our dependencies
module(function($provide) {
// Fake StoreService Implementation returning a promise
$provide.value('StoreService', {
listStores: function() {
return {
then: function(callback) {return callback([{ some: "thing", hoursInfo: {isOpen: true}}]);}
};
},
chooseStore: function() { return null;}
});
// Fake Contact Implementation return an empty object
$provide.value('Contact', {
retrieveContactInfo: function() {
return {
then: function(callback) { return callback({});}
};
}
});
return null;
});
});
beforeEach(function() {
// When Angular Injects the StoreService and Contact dependencies,
// it will use the implementation we provided above
inject(function($controller, $rootScope, _StoreService_, _Contact_) {
scope = $rootScope.$new();
StoreService = _StoreService_;
Contact = _Contact_;
createController = function(params) {
return $controller("StoresCtrl", {
$scope: scope,
$stateParams: params || {}
});
};
});
});
it("should call the store service to retrieve the store list", function() {
var user = { address: {street: 1}};
// Jasmine spy over the listStores service.
// Since we provided a fake response already we can just call through.
spyOn(StoreService, 'listStores').and.callThrough();
// Jasmine spy also allows to call Fake implementations via the callFake function
// or we can return our own response via 'and.returnValue
// Here we can override the response we previously defined and return a promise with a user object
spyOn(Contact, 'retrieveContactInfo').and.callFake(function() {
return {
then: function(callback) { return callback(user); }
};
});
createController();
// Since we setup a spy we can now expect that spied function to have been called
// or to have been called with certain parameters..etc
expect(StoreService.listStores).toHaveBeenCalled();
});
});
Jasmine测试ng Promises - Provide and Spy的更多相关文章
- 用Karma和Jasmine测试Angular应用
TEST: Before you've written any of the code, you know how you want it to behave. You have a specific ...
- AngularJS测试二 jasmine测试路由 控制器 过滤器 事件 服务
测试应用 1.测试路由 我们需要检测路由是否在运作,是否找到了,或者是404了.我们要确认路由事件触发了,预期的模板是否真的加载了.既然路由会改变页面的地址(URL)和页面内容,我们需要检测路由是否被 ...
- Karma+Jasmine测试环境搭建
1.如果你还没安装node的话,去这里下载:http://nodejs.cn/download/,选择跟你电脑匹配的并进行安装,一路next下来就行,路径最好改成自己让自己舒服的,默认的路径可能会很让 ...
- angularJS测试一 Karma Jasmine Mock
AngularJS测试 一 测试工具 1.NodeJS领域:Jasmine做单元测试,Karma自动化完成单元测试,Grunt启动Karma统一项目管理,Yeoman最后封装成一个项目原型模板,npm ...
- angular测试-Karma + Jasmine配置
首先讲一下大致的流程: 需要node环境,首先先要安装node,node不会?请自行搜索.版本>0.8 安装node完成之后先要测试下npm是否测试通过,如下图所示 首先看下目录结构 目录为:F ...
- Karma和Jasmine自动化单元测试——本质上还是在要开一个浏览器来做测试
1. Karma的介绍 Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma.Karma是一个让人感到非常神秘的 ...
- angularjs自动化测试系列之jasmine
angularjs自动化测试系列之jasmine jasmine参考 html <!DOCTYPE html> <html lang="en"> <h ...
- Jasmine入门(结合示例讲解)
参考: http://www.cnblogs.com/wushangjue/p/4541209.html http://keenwon.com/1191.html http://jasmine.git ...
- jasmine —— Spies(转)
Jasmine有称为间谍(spies)的测试双重功能.一个spy可以监测任何函数的调用和参数的调用痕迹.Spy只能存在于定义它的describe()和it()代码块内,而在每一个spec(即it)结束 ...
随机推荐
- Markdown: 用写代码的思维写文档
作者:吴香伟 发表于 2014/08/07 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 本文不讲解Markdown的语法规则,只关注它带来的好处以及我使用的方 ...
- 如何在一个div标签里显示出另一个网页? <iframe src=" http://www.baidu.com " width="800px" height="200px" scrolling="no" frameborder="0"> </iframe>
如何在一个div标签里显示出另一个网页? 用在div里用iframe,就像下面的代码 <iframe src=" http://www.baidu.com " width=& ...
- cx_freeze安装使用
在windows下 安装比较方便又专门的安装包 然后cmd 进入 c:\python33\Scripts >cxfreeze E:\main.py --target-dir E:\ 前面是源文件 ...
- Asp.net Mvc 身份验证、异常处理、权限验证(拦截器)实现代码
本问主要介绍asp.net的身份验证机制及asp.net MVC拦截器在项目中的运用.现在让我们来模拟一个简单的流程:用户登录>权限验证>异常处理 1.用户登录 验证用户是否登录成功步骤直 ...
- winform初学
一.建立winform工程项目
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- checkbox、全选反选,获取值
<input id="Chk_All" onclick="CheckAll()" type="checkbox" /> < ...
- 用户行为数据采集核心思维(APP、web数据采集/埋点)
关于数据采集(也就是所谓的埋点),有很多中形式,或者说方法.所有的数据采集都时围绕一个核心的三个点来做区别的处理. 数据采集核心思维三个点: 1.对象: 要采集谁,一个页面.一个按钮,页面或者按钮,就 ...
- EDA技术与ASIC设计和FPGA开发有什么关系?FPGA在ASIC设计中有什么用途?
利用EDA技术进行电子系统设计的最后目标是完成专用集成电路ASIC的设计和实现:FPGA和CPLD是实现这一途径的主流器件.FPGA和CPLD通常也被称为可编程专用IC,或可编程ASIC.FPGA和C ...
- HTML5中的 Canvas
什么是Canvas? Canvas元素是HTML5的一部分,允许脚本语言动态渲染位图像.Canvas由一个可绘制地区HTML代码中的属性定义决定高度和宽度.JavaScript代码可以访问该地区,通过 ...