博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jasmine测试ng Promises - Provide and Spy
阅读量:5811 次
发布时间:2019-06-18

本文共 3049 字,大约阅读时间需要 10 分钟。

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/p/4000473.html,如需转载请自行联系原作者

你可能感兴趣的文章
海贼王十大悲催人物
查看>>
org.hibernate.MappingException: No Dialect mapping for JDBC type: -1 搞定!
查看>>
热点热词新闻资讯API开放接口(永久免费开放)
查看>>
8.1_Linux习题和作业
查看>>
11.排序算法_6_归并排序
查看>>
Redis redis-cli 命令列表
查看>>
.NET框架设计—常被忽视的框架设计技巧
查看>>
BigDecimal 舍入模式(Rounding mode)介绍
查看>>
开源 免费 java CMS - FreeCMS1.2-标签 infoSign
查看>>
开源 免费 java CMS - FreeCMS1.9 移动APP生成栏目列表数据
查看>>
git reset 三种用法总结
查看>>
hdfs笔记
查看>>
虚拟机新增加硬盘,不用重启读到新加的硬盘
查看>>
Java IO流详尽解析
查看>>
邮件服务系列之四基于虚拟用户的虚拟域的邮件系统(安装courier-authlib以及部分配置方法)...
查看>>
Linux VSFTP服务器
查看>>
DHCP中继数据包互联网周游记
查看>>
Squid 反向代理服务器配置
查看>>
Java I/O操作
查看>>
Tomcat性能调优
查看>>