Jasmine测试ng Promises - Provide and Spy
2024-08-27 17:46:45
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();
});
});
最新文章
- 基于DDD的现代ASP.NET开发框架--ABP系列文章总目录
- OAF_开发系列22_实现OAF条形码BarCode
- 【原】requirejs学习笔记
- ReadOnly关键字修饰的变量可以修改,只是不能重新分配
- 5分钟上手写ECharts的第一个图表
- HDU 小明A+B 2096
- Android列表视图(List View)
- [POJ3279]Fliptile(开关问题,枚举)
- Android开发中如何调用摄像头的功能
- Oracle EBS-SQL (CST-4):检查组织间项目成本.sql
- 【Android进阶】关于PagerAdapter的使用方法的总结
- ShopEx4.8.5.55328破解版
- dp + 组合数 Codeforces Beta Round #9 (Div. 2 Only) D
- Java的类型转换
- WPF自学入门(十一)WPF MVVM模式Command命令
- 阿里云—Gartner 2018 亚太区WAF魔力象限唯一云WAF提供商
- 外星人入侵游戏(python代码)
- struts 1.x 方法探析
- guest project <;web接口开发与自动化测试>;
- python中的os模块
热门文章
- VS2012智能提示消失的解决方法
- base64/62 加解密的实现。
- Maven web项目三种运行方式
- Hadoop2.6.0的FileInputFormat的任务切分原理分析(即如何控制FileInputFormat的map任务数量)
- maven认证信息
- 【环境配置】php5.5 + apache2.4 安装配置【转+修改】
- ros问题总结
- How to copy remote computer files quickly to local computer
- swift基础语法(四) 函数、闭包(Closures)
- HttpURLConnection 文件上传限制