您如何模拟作为函数的 Angular 服务?

How do you mock Angular service that is a function?(您如何模拟作为函数的 Angular 服务?)
本文介绍了您如何模拟作为函数的 Angular 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个我们称之为 CORShttpService 的东西,它基本上是 $http 服务的包装器,但封装了一些我们需要的 CORS 功能.我现在正在为注入了 CORShttpService 的服务编写一些测试.该服务的代码如下:

We have a what we call a CORShttpService, which is basically a wrapper aroung the $http service, but encapsulates some CORS functionality that we need. I'm now writing some tests for a service that has the CORShttpService injected into it. This service has code like this:

CORShttpService({method: requestMethod, url: getUrl(path), data: data}).
    success(function(data, status, headers) {
        //do success stuff
    }).
    error(function(data, status, headers) {
       //do error stuff
    });

我想模拟对 CORShttpService 的调用,但我不知道该怎么做.我正在使用 Jasmine,它的 spyOn 函数需要一个对象来模拟对象上的函数.我的 CORShttpService 没有附加到任何对象,所以我不知道如何去模拟它.是的,我可以使用 $httpBackend 来模拟最终在 CORShttpService 中设置的请求,但我不希望它首先进入该服务.我想隔离单元测试并简单地模拟外部调用.有什么方法可以模拟这个只是一个函数的服务吗?

I want to mock the call to CORShttpService, but I'm not sure how to go about doing it. I'm using Jasmine, and its spyOn function requires an object to mock the function on the object. My CORShttpService isn't attached to any object, so I don't know how to go about mocking it. Yes, I could use $httpBackend to mock the requests that eventually get set in the CORShttpService, but I don't want it going into that service in the first place. I want to isolate the unit test and simply mock the external calls. Is there any way I can mock this service that is just a function?

推荐答案

我想多了,$httpBackend 服务确实提供了很多测试请求的功能.由于我的 CORShttpService 基本上是一个围绕 $http 的包装器,因此我决定如果我对 CORShttpService 进行模拟实现,我可能会物有所值 简单的 $http 实现.使用 本文档 来帮助我,我的规范中有以下内容:

As I thought about this more, the $httpBackend service does provide a lot of functionality for testing requests. As my CORShttpService is a basically a wrapper around $http, I decided I could probably get the most bang for my buck, if I made the mock implementation of CORShttpService simple the $http implementation. Using this documentation to help me, I have the following in my spec:

beforeEach(module(function($provide) {
    $provide.provider('CORShttpService', function() {
        this.$get = function($http) {
            return $http;
        };
    });
}));

所以,我的任何想要注入 CORShttpService 的服务现在基本上只注入了 $http,因此我可以使用所有的 $httpBackend 功能,无需担心 CORShttpService 本身的额外功能.

So, any of my services wanting the CORShttpService injected, will now basically just have $http injected, and thus allow me to use all the $httpBackend functionality without the concern of the extra functionality found in the CORShttpService itself.

这适用于我的具体情况,但就模拟只是一个函数的服务的一般解决方案而言,同样的事情也可以用 zbynour 中提到的 jasmine.createSpy 来完成回答.比如:

This works for my specific case, but as far as a general solution for mocking services that are just a function, the same kind of thing could probably be done with jasmine.createSpy as mentioned in zbynour's answer. Something like:

beforeEach(module(function($provide) {
    $provide.provider('MyService', function() {
        this.$get = function() {
            return jasmine.createSpy("myService");
        };
    });
}));

这篇关于您如何模拟作为函数的 Angular 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)