Jasmine测试ng Promises - Provide and Spy

简介:

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


本文转自破狼博客园博客,原文链接:http://www.cnblogs.com/whitewolf/p/4000473.html,如需转载请自行联系原作者

目录
相关文章
|
30天前
|
JavaScript 测试技术 API
顺藤摸瓜🍉:用单元测试读懂 vue3 中的 provide/inject
顺藤摸瓜🍉:用单元测试读懂 vue3 中的 provide/inject
|
16天前
|
移动开发 JSON Java
Jmeter实现WebSocket协议的接口测试方法
WebSocket协议是HTML5的一种新协议,实现了浏览器与服务器之间的全双工通信。通过简单的握手动作,双方可直接传输数据。其优势包括极小的头部开销和服务器推送功能。使用JMeter进行WebSocket接口和性能测试时,需安装特定插件并配置相关参数,如服务器地址、端口号等,还可通过CSV文件实现参数化,以满足不同测试需求。
78 7
Jmeter实现WebSocket协议的接口测试方法
|
16天前
|
JSON 移动开发 监控
快速上手|HTTP 接口功能自动化测试
HTTP接口功能测试对于确保Web应用和H5应用的数据正确性至关重要。这类测试主要针对后台HTTP接口,通过构造不同参数输入值并获取JSON格式的输出结果来进行验证。HTTP协议基于TCP连接,包括请求与响应模式。请求由请求行、消息报头和请求正文组成,响应则包含状态行、消息报头及响应正文。常用的请求方法有GET、POST等,而响应状态码如2xx代表成功。测试过程使用Python语言和pycurl模块调用接口,并通过断言机制比对实际与预期结果,确保功能正确性。
80 3
快速上手|HTTP 接口功能自动化测试
|
16天前
|
JavaScript 前端开发 测试技术
ChatGPT与接口测试
ChatGPT与接口测试,测试通过
28 5
|
1月前
|
网络协议 测试技术 网络安全
Python进行Socket接口测试的实现
在现代软件开发中,网络通信是不可或缺的一部分。无论是传输数据、获取信息还是实现实时通讯,都离不开可靠的网络连接和有效的数据交换机制。而在网络编程的基础中,Socket(套接字)技术扮演了重要角色。 Socket 允许计算机上的程序通过网络进行通信,它是网络通信的基础。Python 提供了强大且易于使用的 socket 模块,使开发者能够轻松地创建客户端和服务器应用,实现数据传输和交互。 本文将深入探讨如何利用 Python 编程语言来进行 Socket 接口测试。我们将从基础概念开始介绍,逐步引导大家掌握创建、测试和优化 socket 接口的关键技能。希望本文可以给大家的工作带来一些帮助~
|
1月前
|
网络协议 测试技术 网络安全
Python进行Socket接口测试的实现
在现代软件开发中,网络通信是不可或缺的一部分。无论是传输数据、获取信息还是实现实时通讯,都离不开可靠的网络连接和有效的数据交换机制。而在网络编程的基础中,Socket(套接字)技术扮演了重要角色。 Socket 允许计算机上的程序通过网络进行通信,它是网络通信的基础。Python 提供了强大且易于使用的 socket 模块,使开发者能够轻松地创建客户端和服务器应用,实现数据传输和交互。 本文将深入探讨如何利用 Python 编程语言来进行 Socket 接口测试。我们将从基础概念开始介绍,逐步引导大家掌握创建、测试和优化 socket 接口的关键技能。希望本文可以给大家的工作带来一些帮助~
|
1月前
|
SQL Java 测试技术
SpringBoot单元测试快速写法问题之PorkService 接口中的 getPork 方法的作用如何解决
SpringBoot单元测试快速写法问题之PorkService 接口中的 getPork 方法的作用如何解决
|
2月前
|
存储
Postman 接口测试配置 Pre-request Script
Postman 接口测试配置 Pre-request Script
119 5
Postman 接口测试配置 Pre-request Script
|
1月前
|
XML Web App开发 数据挖掘
Postman接口测试工具全解析:功能、脚本编写及优缺点探讨
文章详细分析了Postman接口测试工具的功能、脚本编写、使用场景以及优缺点,强调了其在接口自动化测试中的强大能力,同时指出了其在性能分析方面的不足,并建议根据项目需求和个人偏好选择合适的接口测试工具。
46 1
|
1月前
|
Web App开发 JSON 测试技术
精通Postman接口测试:关联技术与自动化实践指南
这篇文章详细介绍了如何使用Postman进行接口测试,包括关联技术、自动化实践,以及如何通过环境变量和全局变量解决接口之间的关联性问题。
36 0
精通Postman接口测试:关联技术与自动化实践指南