E2E模拟$ httpBackend实际上并不直通适合我 [英] E2E mock $httpBackend doesn't actually passthrough for me

查看:141
本文介绍了E2E模拟$ httpBackend实际上并不直通适合我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我相信我在这里继说明设置$ httpBackend通过选择请求服务器,这不是为我工作。

Although I believe I'm following the instructions here for setting up $httpBackend to pass selected requests to the server, it's not working for me.

这里是一个失败的测试 一个Plunkr,显示我在做什么,并解释评论什么,似乎是想错了。

Here is a Plunkr with a failing test that shows what I'm doing and explains in comments what seems to be going wrong.

我的洞穴探险表明,由于某些原因,模拟 $ httpBackend 没有真正的 $ httpBackend ,这样,当谈到时间通过XHR请求,将其传递到模拟 $ httpBackend 来代替。这第二个电话会抛出一个异常,因为它不知道如何处理的要求做的。

My spelunking suggests that, for some reason, the mock $httpBackend doesn't have an inner copy of the real $httpBackend so that, when it comes time to pass through the XHR request, it passes it to the mock $httpBackend instead. That second call throws an exception because it doesn't know what to do with the request.

我记得AP preciation你对中途测试后。你确定单位 - 和端到端的测试介于集成测试的一个重要范围。我站在上的中间地带。

I remember with appreciation your post on midway testing. You identify an important range of integration testing that falls between unit- and E2E-testing. I am standing on that middle ground.

我不认为你正在表露无疑的。你的答案是完美的合理......或者它的将是合理如果它不被的API参考/ ngMockE2E / $ httpBackend 。我引述如下:

I don't think you are being snarky at all. Your answer is perfect reasonable ... or it would be reasonable if it weren't contradicted by the text of the "API reference / ngMockE2E / $httpBackend". I quote:

这实现可用于与通过 API和它的快捷方式( whenGET 静态或动态响应响应, whenPOST 等)和可选择通过请求到真正的 $ httpBackend 的具体要求(例如,某些远程交互的API或从网络服务器获取模板) ...

This implementation can be used to respond with static or dynamic responses via the when api and its shortcuts (whenGET, whenPOST, etc) and optionally pass through requests to the real $httpBackend for specific requests (e.g. to interact with certain remote apis or to fetch templates from a webserver) ...

[Ⅰ] n的端部到端测试场景的或当应用程序正在与替换真实后端API开发的方案的模拟的,它往往是某些类优选请求绕过模拟和 问题真正的http请求 ....要与这种行为配置后端使用直通的时候,而不是响应 [重点煤矿。

[I]n an end-to-end testing scenario or in a scenario when an application is being developed with the real backend api replaced with a mock, it is often desirable for certain category of requests to bypass the mock and issue a real http request .... To configure the backend with this behavior use the passThrough request handler of when instead of respond.[emphasis mine].

该文档是E2E $ httpBackend 使用情况的茉莉花环境中的事情沉默。我想不出什么理由来了preclude它。如果有这样一个原因,他们应该清楚地说明它。说真的,谁读有关的模拟的组件,并在测试环境中使用它没有预见?

The documentation is silent on the matter of E2E $httpBackend usage within a Jasmine environment. I can't think of a reason to preclude it. If there is such a reason they should state it clearly. Seriously, who reads about a mock component and doesn't anticipate using it in a test environment?

要的通过请求到真正的 $ httpBackend 的具体要求,如与某些远程API的交互的是precisely我打算做的事。还有什么比他们可能是通过真正的$ httpBackend除了非模拟版本的组件呢?

To "pass through requests to the real $httpBackend for specific requests, e.g. to interact with certain remote apis" is precisely what I intend to do. What could they possibly mean by "real $httpBackend" except the non-mock version of that component?

我不明白你的主张

ngMocksE2E 模块的设计是对事物在实际应用的角度正在执行服务器端使用。

The ngMocksE2E module is designed to be used on the "server" side of things where the actual angular application is executing.

服务器出现这个词在该网页上正好3次,没有一次暗示的任何的应用code将服务器上执行。我不知道你的实际应用的角度上执行的意思是事物的服务器的一面。

The word "server" appears exactly 3 times on that page, not once suggesting that any application code would be executed on a "server". I don't know what you mean by the "actual angular application" executing on "the 'server' side of things."

该文档是非常清楚的E2E $ httpBackend 不限于端到端的测试。这也是的当应用程序被用替代真正的后台API开发方案中的模拟的。

The documentation is perfectly clear that the E2E $httpBackend is not limited to E2E testing. It is also for "a scenario when an application is being developed with the real backend api replaced with a mock".

这只是一个半步远离我的方案,其中的应用程序正在测试与真正的后台API 的。

That's just a half step away from my scenario in which an application is being tested with the real backend api."

在我的情况下,在SUT是在其从服务器获取数据的组件调用。我的测试中存在以验证这取决于组件在作出真正的后端这样的请求成功,并且将检索或者按预期的方式保存数据。这是一个集成测试不能由嘲笑后端的行为得到充分满足。

In my scenarios, the SUT is calling upon a component which fetches data from a server. My tests exist to verify that this dependent component succeeds in making such requests of the real backend and will retrieve or save data in the expected manner. This is an integration test that cannot be adequately satisfied by mocking the behavior of the backend.

当然,我可以测试(做测试)与模拟XHR响应组件的正常反应能力,以我的 predict 的将是后端的行为。这是不一样的验证该组件适当响应的实际的后端的行为...这可能作为应用程序的发展变化,从在一些显著方式嘲笑响应出发。

Of course I can test (and do test) with mock XHR responses the component's ability to respond properly to what I predict will be the backend's behavior. That is not the same as validating that the component responds appropriately to the actual backend's behavior ... which might change as the application evolves and depart from the mocked responses in some significant way.

我会考虑使用你中途测试仪用于此目的,如果我知道如何将其交换到SUT的code路径。我不。我觉得做XHR请求该组件是无法访问到你的 ngMidwayTester 。但我不知道如何来干扰真正XHR帮手进入管道,如果我不得不这样做。

I would consider using your midway tester for this purpose if I understood how to swap it into the SUT's code path. I don't. I think the component making XHR requests is inaccessible to your ngMidwayTester. But I do know how to jam a real XHR helper into the pipeline if I have to.

这里就是我站在那一刻

要么有人可以显示如何让 $ httpBackend 通过向服务器传递某些请求 - 作为文档宣称它可以 - 否则我将取代直通实施自己有工作XHR实现。

Either someone can show how to make $httpBackend pass certain requests through to the server - as the documentation proclaims it can - or I will replace the passThrough implementation myself with a working XHR implementation.

我preFER第一个选项。如果驱动到第二,我会为别人谁分享我的需求和API文档的我间pretation的利益提供了一个链接到它这里。

I prefer the first option. If driven to the second, I shall offer a link to it here for the benefit of others who share my needs and my interpretation of the API documentation.

有没有我缺少一个第三的方式?

Is there a 3rd way I'm missing?

推荐答案

以下是目的的解释 $ httpBackend 是在 ngMockE2E 模块。

The following is an explanation of the purpose of the $httpBackend that is in the ngMockE2E module.

ngMockE2E 模块根本就没有设计,也没有打算从茉莉规范中使用。

The ngMockE2E module is simply not designed nor intended to be used from within a jasmine specification.

在做终端到终端的测试也有两个方面的考验。其中之一是,被测试的角应用,另一种是角场景code生活在茉莉花规范。

When doing end-to-end testing there are two sides to the test. One is the angular application that is being tested, the other is the angular-scenario code that lives in the Jasmine Specification.

在端到端测试,没有棱角模块或NG-嘲笑,或任何角度相关的物品(场景亚军除外)茉莉花的一面。

Under E2E tests there are no angular module, or ng-mocks, or anything angular-related on the jasmine side of things (other than the scenario runner).

ngMocksE2E 模块的设计是对在实际的角度执行应用程序的东西服务器端使用。它的主要目的是使我们能够pre-CAN响应,这样的集成级UI测试可以继续比,如果每个页面竟跑到服务器JSON快得多。

The ngMocksE2E module is designed to be used on the "server" side of things where the actual angular application is executing. It's main purpose is to enable us to pre-can responses so that integration-level UI testing can proceed much quicker than if each page actually went to the server for JSON.

在沿着使用茉莉 NG-嘲笑,角将始终与模拟后端更换$ httpBackend。当添加 ngMocksE2E 模块将无法得到阿霍德的任何真正的 $ httpBackend 和你有已经发现了,只是换了模拟,并委托给它的直通。

When using jasmine along with ng-mocks, angular will always replace the $httpBackend with the mock backend. When adding the ngMocksE2E module it will not be able to get ahold of any "real" $httpBackend and as you have already found out, will just wrap the mock and delegate to it on the pass-through.

这似乎是那种测试,你试图写的是一个测试不测试UI集成,但测试应用程序的JavaScript和服务器整合。

It would seem that the kind of test you are trying to write is a test that doesn't test the UI integration, but tests the application javascript and server integration.

这是完全测试的合法风格(简称一些如角社区midwayTesting')。你的问题是你使用了错误的工具。

This is perfectly legitimate style of testing (referred to some as 'midwayTesting' in the angular community). Your problem is you are using the wrong tool.

我将在这看看:

https://github.com/yearofmoo/ngMidwayTester

,你会用,而不是角嘲笑和angular.module()为了方便我假设你想要做的一种测试。

Which you would use instead of angular-mocks and angular.module() in order to facilitate the kind of testing I'm assuming you want to do.

您可以阅读更多关于它在这里:

You can read more about it here:

http://www.yearofmoo.com/2013/01/full-spectrum-testing-with-angularjs-and-karma.html

(道歉,如果你已经被挂在那里)

(apologies if you have already been linked there)

编辑:(要解决的问题更多的评论)

(To address additional comments in question)

您有一个真正的牛肉,该文件并不清楚 ngMockE2E 无法在客户端上使用一个终端到终端(即因果报应/茉莉)一侧测试设置。就像你有间preTED他们来说,这不是没有道理的跨$ P $磅的东西,但它不会改变的事实,国米pretation是错误的。

You have a real beef in that the documentation is not clear that ngMockE2E cannot be used on the client (i.e. karma/jasmine) side of an end-to-end testing setup. It is not unreasonable to interpret things like you have interpreted them, but it doesn't change the fact that the interpretation is wrong.

的ngMockE2E将通过请求如果上的应用程序的服务器端,而不是在客户端侧使用时,指示。这是为了让你仍然可以通过,是很难嘲笑为pre-罐装响应的请求。我的意思是客户端和服务器端的是,终端到终端的测试有两个目的。你必须要测试它是由一个标准的应用服务器提供的应用程序,并且有测试code正在推动通常是在因果报应或其他测试运行执行的应用程序,它使用标准的HTTP请求传达给应用程序,在另一个进程正在执行。

The ngMockE2E will pass through requests if instructed when used on the server side of an application rather than on the client side. This is meant so that you can still pass through certain requests that are hard to mock as pre-canned responses. What I mean by client and server-side is that in end-to-end testing there are two ends. You have the application to be tested which is served by a standard application server, and you have the test code that is driving the application usually executing in Karma or another test runner, which uses standard HTTP requests to communicate to the application which is executing in another process.

如果你看一下文档,以及如何设置 ngMockE2E ,你会发现没有茉莉花提,和指令是如何在实际的角度设置应用程序:

If you look at the documentation and how to setup ngMockE2E you will notice there is no mention of Jasmine, and the instructions are for how to set up in a real angular application:

myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
myAppDev.run(function($httpBackend) {
  phones = [{name: 'phone1'}, {name: 'phone2'}];

  // returns the current list of phones
  $httpBackend.whenGET('/phones').respond(phones);

  // adds a new phone to the phones array
  $httpBackend.whenPOST('/phones').respond(function(method, url, data) {
    phones.push(angular.fromJson(data));
  });
  $httpBackend.whenGET(/^\/templates\//).passThrough();
  //...
});

正如你可以看到在这个例子,他们嘲讽所有JSON数据说明,同时允许它仍然从服务器获取模板。

As you can see in this example, they are mocking all the JSON data instructions, while letting it still fetch the templates from the server.

为了使用它从茉莉设置将是完全不同的,使用 angular.mock.module('ngMockE2E'),然后设置了 $ httpBackend.whenGET() beforeEach(),而不是在 module.run()

In order to use it from jasmine the setup would be quite different, using angular.mock.module('ngMockE2E') and then setting up the $httpBackend.whenGET() in a beforeEach() rather than in a module.run().

至于 ngMidwayTester 我联系你,我相信这会,其实与 ngMockE2E 兼容。从本质上讲 ngMidwayTester 替换 angular.mock.module()注入()与它自己的实现。所以,你可以使用它是这样的:

As far as ngMidwayTester I linked you to, I believe this would, in fact, be compatible with ngMockE2E. Essentially ngMidwayTester replaces angular.mock.module() and inject() with it's own implementations. So you could use it like this:

beforeEach(function(){
  tester = ngMidwayTester('app', 'ngMockE2E');
  $http = tester.inject('$http');
  $httpBackend = tester.inject('$httpBackend');
  $rootScope = tester.inject('$rootScope');
});

这应该工作,因为你不再使用ngMock模块(当你使用它总是被列入 angular.mock.module())。事情应该工作完全像你希望他们使用 ngMidwayTester

This should work, because you are no longer using the ngMock module (which always gets included when you use angular.mock.module()). Things should work exactly like you want them to using ngMidwayTester.

这篇关于E2E模拟$ httpBackend实际上并不直通适合我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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