Jasmine测试ng Promises - Provide and Spy
2014-09-29 16:20 破狼 阅读(2136) 评论(0) 编辑 收藏 举报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();
});
});
作者:破 狼
出处:http://www.cnblogs.com/whitewolf/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客、博客园--破狼和51CTO--破狼。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架