Files
freeCodeCamp/curriculum/challenges/japanese/06-quality-assurance/quality-assurance-and-testing-with-chai/run-functional-tests-on-an-api-response-using-chai-http-iii---put-method.md
2022-01-20 20:30:18 +01:00

4.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d824f367417b2b2512c5a Chai-HTTP を使用して API レスポンスで機能テストを実行するⅢ - PUT メソッド 2 301590 run-functional-tests-on-an-api-response-using-chai-http-iii---put-method

--description--

注意点として、このプロジェクトは Replit の始動プロジェクト、または GitHub からクローンされたプロジェクトに基づいて構築されています。

PUT リクエストをテストする時、一緒にデータを送信することがよくあります。 PUT リクエストに含まれるデータは、リクエストボディと呼ばれます。

PUT リクエストと JSON オブジェクトを '/travellers' エンドポイントへ送信するために、chai-http プラグインの put メソッドと send メソッドを使用することができます。

chai
  .request(server)
  .put('/travellers')
  .send({
    "surname": [last name of a traveller of the past]
  })
  ...

するとルートは次のように応答します。

{
  "name": [first name],
  "surname": [last name],
  "dates": [birth - death years]
}

'/travellers' エンドポイントへの異なるレスポンスについては、サーバーのコードを参照してください。

--instructions--

tests/2_functional-tests.js 内で、'Send {surname: "Colombo"}' テスト (// #3) を変更し、put メソッドと send メソッドを使用して '/travellers' エンドポイントをテストしてください。

PUT リクエストで、次の JSON オブジェクトを送信してください。

{
  "surname": "Colombo"
}

request.end コールバック内で、以下を確認してください。

  1. status は、200 でなければなりません。
  2. type は、application/json でなければなりません。
  3. body.name は、Cristoforo でなければなりません。
  4. body.surname は、Colombo でなければなりません。

上記のアサーション順序に従ってください (この順序を想定しています)。 また、完了したら必ず assert.fail() を削除してください。

--hints--

すべてのテストに合格する必要があります。

(getUserInput) =>
  $.get(getUserInput('url') + '/_api/get-tests?type=functional&n=2').then(
    (data) => {
      assert.equal(data.state, 'passed');
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

res.status が 200 かどうかをテストする必要があります。

(getUserInput) =>
  $.get(getUserInput('url') + '/_api/get-tests?type=functional&n=2').then(
    (data) => {
      assert.equal(data.assertions[0].method, 'equal');
      assert.equal(data.assertions[0].args[0], 'res.status');
      assert.equal(data.assertions[0].args[1], '200');
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

res.type'application/json' かどうかをテストする必要があります。

(getUserInput) =>
  $.get(getUserInput('url') + '/_api/get-tests?type=functional&n=2').then(
    (data) => {
      assert.equal(data.assertions[1].method, 'equal');
      assert.equal(data.assertions[1].args[0], 'res.type');
      assert.match(data.assertions[1].args[1], /('|")application\/json\1/);
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

res.body.name'Cristoforo' かどうかをテストする必要があります。

(getUserInput) =>
  $.get(getUserInput('url') + '/_api/get-tests?type=functional&n=2').then(
    (data) => {
      assert.equal(data.assertions[2].method, 'equal');
      assert.equal(data.assertions[2].args[0], 'res.body.name');
      assert.match(data.assertions[2].args[1], /('|")Cristoforo\1/);
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

res.body.surname'Colombo' かどうかをテストする必要があります。

(getUserInput) =>
  $.get(getUserInput('url') + '/_api/get-tests?type=functional&n=2').then(
    (data) => {
      assert.equal(data.assertions[3].method, 'equal');
      assert.equal(data.assertions[3].args[0], 'res.body.surname');
      assert.match(data.assertions[3].args[1], /('|")Colombo\1/);
    },
    (xhr) => {
      throw new Error(xhr.responseText);
    }
  );

--solutions--

/**
  Backend challenges don't need solutions, 
  because they would need to be tested against a full working project. 
  Please check our contributing guidelines to learn more.
*/