Protractor, mocking backend with angular2 on api request

后端 未结 5 870
执念已碎
执念已碎 2020-12-14 07:50

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:<

5条回答
  •  春和景丽
    2020-12-14 08:04

    Protractor does not yet support adding mock modules for Angular 2 applications:

    // TODO: support mock modules in Angular2. For now, error if someone
    // has tried to use one.
    if (self.mockModules_.length > 1) {
      deferred.reject('Trying to load mock modules on an Angular2 app ' +
          'is not yet supported.');
    }
    

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

    • Support adding mock modules to Angular 2 applications

    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:

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

    Same is true for the http-backend-proxy and httpbackend packages.


    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

提交回复
热议问题