2016-10-18 3 views
2

で別の動詞を持つすべてのリクエストです:インターセプトは、これは私のインターセプト機能の一つは、今どのように見えるか矢筈

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

どうすればいいですか?

答えて

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));  
} 
思い付いたものです
関連する問題