4

これは、私のインターセプト関数の1つが現在どのように見えるかです:

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)
}

そうする方法はありますか?

4

1 に答える 1

0

これが私が思いついたものです:)

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));    
}
于 2017-07-31T16:27:13.943 に答える