Using ngMock for backend-less testing an external

2019-09-15 04:54发布

问题:

Is there a way to use angular-mocks.js to invoke backend-less request mocking, testing an external site? There are plenty of examples using angular mocks on a codebase that the developer has access to add a . However, I'm stuck in a scenario... calling out to an Angular site, in the wild, which I need to test the frontend functionality, with well-known endpoints/results.

A quick fake example:

  1. Call out to www.externalwebsite.com/RealAngularApp
  2. RealAngularApp calls an endpoint www.externalwebsite.com/api/v1/getdata that returns { id: 1, name: "testing" }
  3. I need to intercept this (as angular mocks does), but without the ability to add the angular mocks script tag in the code (because it's an external site)

Any/all thoughts are welcome.

Thank you.

回答1:

You could use the https://github.com/atecarlos/protractor-http-mock library which lets you intercept HTTP requests and serve predefined responses. Great thing about this library is that you don't have to modify your application code.