Sinon catch xmlhttprequest

I have a question about testing Sinon. I am using mochacasper and I want to catch the XMLHttpRequest that is sent after the button is clicked. Until I can grasp the request ...

casper.then(function () {
  this.fill(...);
});
casper.then(function() {
  this.click('#myButton');
});
casper.then(function () {
  //I want to catch the request/response here
});

      

Since I am using my test environment database, I know that my request has been sent because my test data is being added to my database. I've tried sinon useFakeXMLHttpRequest

as well sinon fakeServer

, but none works.

So how can I catch the request?

+3


source to share


1 answer


I worked out a "doable" result with Artjom B.'s comment . This is not exactly what I need, but for the moment it is enough ...

This is how I did it:



casper.then(function () {
this.click('button[data-hook="submitButton"]');
});
casper.waitForResource(function (resource) {
 return resource.status === 201;
}, function () {
this.echo('Appointment has been created');
});

      

The only problem is that I cannot access the body of the response, but at least I can verify that the state is correct.

0


source







All Articles