如何验证量角器测试$ httpBackend模拟期望? [英] How to verify $httpBackend mock expectations in a Protractor test?

查看:244
本文介绍了如何验证量角器测试$ httpBackend模拟期望?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个REST API一个前端。我使用的终端到终端的测试量角器与API嘲笑,因为我希望能够测试前端隔离。

I'm developing a frontend for a REST API. I'm using Protractor for end-to-end tests with the API mocked out because I want to be able to test the frontend in isolation.

下面是一个简单的测试,我得到了:

Here's a simple test I got:

describe('partner', function () {
    it('should list partners', function () {
        var page = new PartnerListPage();
        var httpBackendMock = function () {
            angular.module('httpBackendMock', ['ngMockE2E'])
                .run(function ($httpBackend) {
                    $httpBackend.whenGET('/api/partners').respond(200, {
                        _embedded: {
                            partners: [
                                {
                                    firstName: 'Elnur',
                                    lastName: 'Abdurrakhimov'
                                }
                            ]
                        }
                    });
                    $httpBackend.whenGET(/.*/).passThrough();
                });
        };
        browser.addMockModule('httpBackendMock', httpBackendMock);

        page.open();
        expect(page.isOpen()).toBeTruthy();
        expect(page.getPartner(0).firstName).toBe('Elnur');
        expect(page.getPartner(0).lastName).toBe('Abdurrakhimov');
    });
});

他们做的,这样它返回的东西,我希望它基本上是建立后端的模拟。

What it does is basically create a mock of the backend so that it returns something I want it to.

这一切都很好,除了一件事花花公子。我希望能够在下面的方法调用在测试结束:

It's all fine and dandy except for one thing. I want to be able to call the following methods at the end of the test:

$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();

我想这样做的原因是为了验证模拟正在得到预期的请求。它没有为我提供的测试多大意义,但让我们说,我想验证,当我提出某种形式的,特别是数据的特定 POST 请求被执行。我想对后端验证嘲笑自己,这样我的测试是从输出的 POST 请求的响应的结果,因为这将在另一个测试进行测试隔离。

The reason I want to do that is to verify that the mock is getting the expected requests. It doesn't make much sense for the test I've provided, but let's say I want to verify that when I submit some form, a particular POST request with particular data is being executed. I want to do the verification on the backend mock itself so that my test is isolated from outputting the result of the response of that POST request because that will be tested in another test.

我如何获得访问该公司在 httpBackendMock 我添加使用相同的 $ httpBackend 实例到浏览器

How do I get access to the same $httpBackend instance that's being used in the httpBackendMock I've added to browser?

据我了解,量角器和应用程序本身运行在单独的进程。因此,我将无法访问到 $ httpBackend 直接以同样的方式,我可以在一个单元测试。

As far as I understand, Protractor and the app itself run in separate processes. Therefore I can't get access to $httpBackend directly the same way I can in a unit test.

推荐答案

我发现不是使用 $ httpBackend 办法一个更好的解决方案。

I've found a much better solution than using the $httpBackend way.

既然你运行的Node.js量角器测试中,它更有意义,只是用一些Node.js的包嘲笑后端,而不是我的问题,提出了这一丑恶和哈克的解决方案。

Since you run Protractor tests on Node.js, it makes more sense to just use some Node.js package to mock the backend instead of this ugly and hacky solution I proposed in the question.

为我工作的伟大,所以我建议使用它,或者类似的东西。

Hock worked great for me and hence I recommend using it or something similar.

这篇关于如何验证量角器测试$ httpBackend模拟期望?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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