在 nock 中拦截所有带有不同动词的请求

Intercept all requests with different verbs in nock

这是我的一个拦截函数现在的样子:

interceptWithError() {
  nock(baseUrl)
    .get(/.*/)
    .replyWithError(500);

  nock(baseUrl)
    .put(/.*/)
    .replyWithError(500);

  nock(baseUrl)
    .post(/.*/)
    .replyWithError(500);

  nock(baseUrl)
    .delete(/.*/)
    .replyWithError(500);
}

我想避免重复,并通过做这样的事情给它更多的灵活性:

interceptWithError(params) {
  const verb = params && params.verb;
  const stat = params && params.stat;

  return nock(baseUrl)
    .[verb]    // something like this!!!
    .replyWithError(stat)
}

有办法吗???

这是我想出的:)

baseNock(url) {
  return this.nock(url)
    .replyContentLength()
    .defaultReplyHeaders({ 'Content-Type': 'application/json' });
}

interceptWithError(verbCodeMap) {
  const verbs = (verbCodeMap && Object.keys(verbCodeMap))
    || ['post', 'get', 'put', 'delete'];

  return verbs.map(verb => 
    baseNock(someUrl)[verb](/.*/)
      .replyWithError((verbCodeMap && verbCodeMap[verb]) || 500));    
}