如何使用 sinon.fakeServer 响应请求正文?

How to respond with request body using sinon.fakeServer?

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");