2017-02-02 15 views
5

私はmochaをテストフレームワークとして使用していますが、fetchを使用してDELETE要求をモックアップしようとしていますが、HTTPステータスコード204を返します。あなたはlog()isDone()矢筈法により述べたように、要求が適切に傍受されて見ることができるようにNockは要求を傍受しますが空のオブジェクトを返します

matching <domain> to DELETE <domain>/<path>: true 
(the above line being generated by the .log method in nock) 
IS DONE?---> true 
RESPONSE---> {} 

、:ここで

は、テストコードです:

it('should logout user', (done) => { 
    nock(<domain>) 
    .log(console.log) 
    .delete(path) 
    .reply(204, { 
     status: 204, 
     message: 'This is a mocked response', 
    }); 

    api.logout(token) 
    .then((response) => { 
     console.log('IS DONE?--->', nock.isDone()); 
     console.log('RESPONSE--->', response); 
     done(); 
    }) 
    .catch((error) => { 
     console.log('ERROR--->', error); 
    }); 
}); 

これは次の出力を返します。しかし、返されたresponseオブジェクトは空のオブジェクトなので、返されたHTTPステータスコード(この例では204)についてアサーションを行うことはできません

私はここで何が不足しているか考えていますか?reply()メソッドが空のオブジェクトを返すのはなぜですか?ここ

UPDATE

remove方法がDELETE HTTPメソッドを使用してfetch要求のラッパーであり、logoutメソッドのコードです。

logout(token) { 
    return remove(
    this.host, 
    END_POINTS.DELETE_TOKEN, 
    { 
     pathParams: { token }, 
    }, 
    { 
     Accept: 'application/json', 
     'Content-Type': 'application/json', 
     Authorization: `Bearer ${token}`, 
    }, 
); 
} 
+0

'api.logout'のコードは何ですか?それは 'ノック 'によって提供されるものではないようです。そこには誤りがあるかもしれないと私には思われます。 – Louis

+0

エンドポイントの 'DELETE'HTTPメソッドを表します。これは正常に動作し、出力に示されているように正しく傍受されているように見えます。コードを含めるように質問を更新しました。混乱を避ける。 – rfc1484

答えて

1

私はあなたが200にコースのサーバーを、それを変更する必要がありますので、204は、応答bodyを持つことが想定されていないと思いますが、応答を返すことができますが、私はフェッチがそれを処理できないと思います。 requestのような他のパッケージは204ステータスのボディを扱いますが、このリクエストパッケージはサーバ側のみです。

また、あなたのラッパーが何をしているのかわかりませんが、私はresponse.json()promiseを使ってレスポンスを取得する必要があると思います。また、モカは自動的に約束を処理することができます。返すことができます。

const nock = require('nock') 
const fetch = require('isomorphic-fetch'); 
const request = require('request') 

const domain = "http://domain.com"; 
const path = '/some-path'; 
const token = 'some-token'; 

const api = { 
    logout: (token) => { 
     return fetch(domain + path, { 
      method: 'DELETE', 
      headers: { 
       'Content-Type': 'application/json' 
      } 
     }); 
    } 
} 

describe('something',() => { 
    it('should logout user with 204 response using request package', (done) => { 
     nock(domain) 
      .log(console.log) 
      .delete(path) 
      .reply(204, { 
       status: 204, 
       message: 'This is a mocked response', 
      }); 

     request.delete(domain + path, function(err, res) { 
      console.log(res.body); 
      done(err); 
     }) 
    }); 

    it('should logout user',() => { 
     nock(domain) 
      .log(console.log) 
      .delete(path) 
      .reply(200, { 
       status: 200, 
       message: 'This is a mocked response', 
      }); 

     return api.logout(token) 
      .then((response) => { 
       console.log('IS DONE?--->', nock.isDone()); 
       return response.json(); 
      }) 
      .then(function(body) { 
       console.log('BODY', body); 
      }) 
      .catch((error) => { 
       console.log('ERROR--->', error); 
      }); 
    }); 
}); 

この意志出力:

something 
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true 
{"status":204,"message":"This is a mocked response"} 
    ✓ should logout user with 204 response 
matching http://domain.com:80 to DELETE http://domain.com:80/some-path: true 
IS DONE?---> true 
BODY { status: 200, message: 'This is a mocked response' } 
    ✓ should logout user 

使用depsの後:私はそれが役に立てば幸い

"dependencies": { 
    "isomorphic-fetch": "^2.2.1", 
    "mocha": "^3.2.0", 
    "nock": "^9.0.6", 
    "request": "^2.79.0" 
    } 

以下の完全な例を参照してください。

関連する問題