Javascript Sinon捕获xmlhttprequest
我有一个关于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 }); 因为
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,而是自己创建的。也许它适用于您的用例: