你怎么嘲笑角服务,是一个功能? [英] How do you mock Angular service that is a function?

查看:80
本文介绍了你怎么嘲笑角服务,是一个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有我们所说的 CORShttpService ,这基本上是aroung的 $ HTTP 服务的包装,但封装了一些功能CORS是我们需要的。我现在正在写一些测试具有 CORShttpService 注入到它的服务。该服务有code是这样的:

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 ,但我不知道如何去这样做。我使用的茉莉花,其 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.

这适用于我的具体情况,但就为嘲笑那只是一个功能,同样的事情也可能与 jasmine.createSpy 在zbynour的答复中提到。是这样的:

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");
        };
    });
}));

这篇关于你怎么嘲笑角服务,是一个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆