Sinon documentation声明可以访问请求对象:
server.respondWith(response);
[...]
When the response is a
Function
, it will be passed the request object. You must manually call respond on it to complete the request.
但天真的方法似乎并不奏效:
const server = sinon.server.create();
server.respondWith(request => request.requestBody);
(在我的 Mocha 套件中将响应作为错误抛出)。
最佳答案
您需要添加server.respond();
。之后,您将拥有 server.requests
对象。
例如在 qunit 中:
server.respond([200, { "Content-type": "application/json" }, "OK"]);
assert.ok(server.requests.length > 0, "Response received");
assert.ok(server.requests[0].status == 200, "Status is 200");
关于javascript - 如何使用 sinon.fakeServer 响应请求体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45163874/