Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Sinon捕获xmlhttprequest_Javascript_Jquery_Mocha.js_Casperjs_Sinon - Fatal编程技术网

Javascript Sinon捕获xmlhttprequest

Javascript Sinon捕获xmlhttprequest,javascript,jquery,mocha.js,casperjs,sinon,Javascript,Jquery,Mocha.js,Casperjs,Sinon,我有一个关于Sinon测试的问题。我正在使用,希望捕获单击按钮后发送的XMLHttpRequest。到目前为止,我还没有接到这个请求 casper.then(function () { this.fill(...); }); casper.then(function() { this.click('#myButton'); }); casper.then(function () { //I want to catch the request/response here }); 因为

我有一个关于Sinon测试的问题。我正在使用,希望捕获单击按钮后发送的XMLHttpRequest。到目前为止,我还没有接到这个请求

casper.then(function () {
  this.fill(...);
});
casper.then(function() {
  this.click('#myButton');
});
casper.then(function () {
  //I want to catch the request/response here
});
因为我使用的是测试环境数据库,所以我知道我的请求已发送,因为我的测试数据已添加到数据库中。 我试过
sinon的usefakemmlhttprequest
,也试过
sinon的fakeServer
,但都不起作用

那么,我怎样才能抓住这个请求呢?

我已经通过他的评论得出了一个“可行”的结果。 这并不完全是我所需要的,但现在已经足够了

我是这样做的:

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

唯一的问题仍然是,我无法访问响应的主体,但至少我可以检查状态是否正确。

不是使用SinonJS,而是自己创建的。也许它适用于您的用例: