vendredi 4 décembre 2015

Mocking the HTTP backend in JavaScript protractor end to end testing

I have some end to end test (JavaScript + Protractor) and I need to mock an API backend.

I'm using http-backend-proxy:

That's what I did:

var HttpBackend = require('http-backend-proxy');
var myData = require('myFakeApiResponse.json');

this.proxy = new HttpBackend(browser);  
this.proxy.whenGET(/.+\/api\/groups\/.+/).respond(200, myData);

JavaScript complains that whenGET is not a function.

If I do like this:

this.proxy.whenGET(/.+\/api\/groups\/.+/);

It doesn't complain anymore (but obviously I need to set the response so I need the .respond() part)

I can't understand why it doesn't work. The proxy object seems to be set, when I console.log it I get:

{ when: [Function],
  whenGET: [Function],
  whenPUT: [Function],
  whenHEAD: [Function],
  whenPOST: [Function],
  whenDELETE: [Function],
  whenPATCH: [Function],
  whenJSONP: [Function],
  context: {},
  flush: [Function],
  syncContext: [Function],
  onLoad: [Getter] }

it seems to be a proper JavaScript object (even the whenGET() function is listed!)

Aucun commentaire:

Enregistrer un commentaire