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();
});
});

原文地址:Testing Promises with Jasmine – Provide and Spy

Jasmine测试ng Promises - Provide and Spy的更多相关文章

  1. 用Karma和Jasmine测试Angular应用

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

  2. AngularJS测试二 jasmine测试路由 控制器 过滤器 事件 服务

    测试应用 1.测试路由 我们需要检测路由是否在运作,是否找到了,或者是404了.我们要确认路由事件触发了,预期的模板是否真的加载了.既然路由会改变页面的地址(URL)和页面内容,我们需要检测路由是否被 ...

  3. Karma+Jasmine测试环境搭建

    1.如果你还没安装node的话,去这里下载:http://nodejs.cn/download/,选择跟你电脑匹配的并进行安装,一路next下来就行,路径最好改成自己让自己舒服的,默认的路径可能会很让 ...

  4. angularJS测试一 Karma Jasmine Mock

    AngularJS测试 一 测试工具 1.NodeJS领域:Jasmine做单元测试,Karma自动化完成单元测试,Grunt启动Karma统一项目管理,Yeoman最后封装成一个项目原型模板,npm ...

  5. angular测试-Karma + Jasmine配置

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

  6. Karma和Jasmine自动化单元测试——本质上还是在要开一个浏览器来做测试

    1. Karma的介绍 Karma是Testacular的新名字,在2012年google开源了Testacular,2013年Testacular改名为Karma.Karma是一个让人感到非常神秘的 ...

  7. angularjs自动化测试系列之jasmine

    angularjs自动化测试系列之jasmine jasmine参考 html <!DOCTYPE html> <html lang="en"> <h ...

  8. Jasmine入门(结合示例讲解)

    参考: http://www.cnblogs.com/wushangjue/p/4541209.html http://keenwon.com/1191.html http://jasmine.git ...

  9. jasmine —— Spies(转)

    Jasmine有称为间谍(spies)的测试双重功能.一个spy可以监测任何函数的调用和参数的调用痕迹.Spy只能存在于定义它的describe()和it()代码块内,而在每一个spec(即it)结束 ...

随机推荐

  1. Sharepoint创建List

    (一)在一个环境下创建site 首先在sharepoint 2013 Central Administration中run administration --Application Managemen ...

  2. shell---变量自增

    我所知道的,bash中,目前有五种方法:1. i=`expr $i + 1`;2. let i+=1;3. (( i++ ));4. i=$[ $i+1 ];5. i=$(( $i + 1 ))

  3. HMC V7.9.0升级至SP2版本

    补丁下载地址:http://www-933.ibm.com/support/fixcentral/ 选择补丁版本 MH01405是和ftp://ftp.software.ibm.com/softwar ...

  4. 《第一本docker书》- 第一章笔记

    环境: Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-30-generic i686) 第一章: 1 Docker客户端和服务器 2 Docker镜像 添加一个文件,执行一 ...

  5. Windows Desktop 调用 WinRT api

    <Reference Include="Windows"> <HintPath>..\..\..\..\..\..\Program Files (x86)\ ...

  6. 面向对象的OOA、OOD、OOP

    OOA Object-Oriented Analysis:面向对象分析方法 是在一个系统的开发过程中进行了系统业务调查以后,按照面向对象的思想来分析问题.OOA与结构化分析有较大的区别.OOA所强调的 ...

  7. wpf 后台绘制圆弧

    wpf 前台绘制圆弧很简单,如:<Path x:Name="path_data" Stroke="#FFE23838" StrokeThickness=& ...

  8. 如何使用ajax将json传入后台数据

    首先采用jquery内部封装好的方法是比较简单的,我们只需做的就是修改里面的一些配置: 对$.ajax()的解析: $.ajax({ type: "POST", //提交方式 co ...

  9. vim的批量注释与删除注释

    vim的批量注释与删除注释 方法一:块选择模式 批量注释: Ctrl + v 进入块选择模式,然后移动光标选中你要注释的行,再按大写的I进入行首插入模式输入注释符号如 // 或 #,输入完毕之后,Vi ...

  10. 绿色ip扫描工具

    ip扫描,可以扫描一下自己的局域网有多少设备在线,有没有在蹭网.优点在于:短小精悍,快速,不用安装. 其实,我自己也写过一个但是没有这个精致,感兴趣的可以一起讨论. 下载地址:32位 64位 我的淘宝 ...