量角器,在api请求上使用angular2模拟后端 [英] Protractor, mocking backend with angular2 on api request

查看:125
本文介绍了量角器,在api请求上使用angular2模拟后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用一些帮助找到解决问题的方法.当我向angular2应用程序发出对api的请求时,我需要模拟一些数据,我需要执行以下操作:

I could use some help to find a solution for my problem. I need to mock some data to my angular2 application when it makes a request to an api, I need to do something like:

$httpBackend.when('GET', '/userbookings/').respond(my json file data);

问题是,我只能使用用于angularJS(角度1)的$ httpBackend在Google上找到所有内容.

The problem is that all I can find on google, using the $httpBackend which is used for angularJS (angular 1).

有人知道如何在E2E测试中使它正常工作吗(该应用程序是angular2应用程序)?我正在尝试使用量角器或守夜器(尝试过两个框架)

Does any know how I can get this to work in my E2E test (The application is an angular2 applciation)? I'm trying to do this with both protractor or nightwatch (Have tried both frameworks)

规格测试:

describe('Protractor Mocking bookings for angular2 site', function() {

var ngMockE2E = require('ng-mock-e2e');

var $httpBackend = ngMockE2E.$httpBackend;

beforeEach(function() {
    ngMockE2E.addMockModule();
    ngMockE2E.addAsDependencyForModule('myApp');
    ngMockE2E.embedScript('/bower_components/angular-mocks/angular-mocks.js');
});


afterEach(function() {
    ngMockE2E.clearMockModules();
});

it('Inject mock data of bookings', function() {

    var EC = protractor.ExpectedConditions;
    var global = require('../bin/globals.js');

    // Bookings data in a json file which should be send as the response
    var mockData = require('../testData.json');

    browser.ignoreSynchronization = false;

    $httpBackend.when('GET', '/userbookings').respond(mockData);

    browser.get(global.so.enLoggedIn);

});

});

此测试无法正常工作,因为它使用了某种angular1方式.已经显示了它,所以您可以看到我的测试样子.

This test wont work because it's using some angular1 way. Have showed it, so you can see how my test looks like.

希望有人可以在这里帮助我,导致很难找到与angular2一起工作的东西.

Hope someone can help me out here, cause its really hard to find some working with angular2.

推荐答案

量角器 而且,我还为TODO创建了一个github问题来引起注意:

And, I've also created a github issue for the TODO to draw attention:

顺便说一句,这也意味着protractor-http-mock将不起作用,因为它依赖于addMockModule

This, by the way, also means that protractor-http-mock is not gonna work since it relies on addMockModule internally. I've personally tried protractor-http-mock on a sample Angular2 application, got:

失败:尚不支持在Angular2应用上加载模拟模块.

Failed: Trying to load mock modules on an Angular2 app is not yet supported.

http-backend-proxy 我想,尽管问题尚未解决,但您应该考虑启动 proxy 来充当API后端的一种外部模拟",但还没有做到这一点就个人而言,请参见:

I guess, while the issue is not yet fixed, you should consider firing up a proxy that would act as a sort of a "external mock" to your API backend, haven't done that personally, see more at:

  • Running AngularJS Protractor with proxy to https
  • Mocking and Stubbing with protractor

这篇关于量角器,在api请求上使用angular2模拟后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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