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. 利用带关联子查询Update语句更新数据

    Update是T-sql中再简单不过的语句了,update table set column=expression  [where condition],我们都会用到.但update的用法不仅于此,真 ...

  2. OSG配置问题

    BUILD_OSG_ARAPPERS: 这一项要选,不然后面编译自己的测试程序的时候,会报无法打开文件"osgIntrospectiond.lib"错误. Could not fi ...

  3. mybatis中表与表之间的关联

    第三天 1.mybatis处理表与表之间的关系? 比如要在帖子回复表里显示其它两张相关联表的信息. 处理的第一种方式: 1)主要的数据实体类是ReplyInfo,相关联的实体表的数据是TitleInf ...

  4. 关于Spring和mybatis的整合

    Spring同Mybatis的整合 1.引入相应的jar包.(Mybatis的jar包,Spring的jar包,mybatis-spring-1.1.1.jar). 2.编写相应的包(三层的包).搭建 ...

  5. C# Form.Close 的释放问题

    今天使用From窗口Close后,发现From的资源还存在,并没有释放资源,只有在程序关闭的时候才去释放. Form1:button按钮 private void button1_Click(obje ...

  6. js 根据名字获取cookie 的方法

    function getcookie(c_name) { if (document.cookie.length > 0) { c_start = document.cookie.indexOf( ...

  7. Qt 为tableview的item添加网格线

    使用qss可以显示每个item的网格: selection-background-color: rgb(170, 170, 127); gridline-color: rgb(255, 255, 25 ...

  8. jQuery学习-css、class操作、动画方法的运用、jQ操作Dom节点

    css操作(设置单个/多个样式.获取样式) //修改单个属性:括号之中直接是需要修改的样式名,值 css(name,value) //例:$("#one").css("b ...

  9. JS-字符串操作-替换

    <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...

  10. Fresco简单的使用—SimpleDraweeView

    本文出处:http://blog.csdn.net/u011164565/article/details/51330778 Fresco是一个第三方库,github官网地址:https://githu ...