freeCodeCamp/curriculum/challenges/portuguese/06-quality-assurance/quality-assurance-and-testi.../run-functional-tests-on-an-...

3.6 KiB

id title challengeType forumTopicId dashedName
587d824f367417b2b2512c5a Executar testes funcionais em uma resposta de API usando Chai-HTTP III - método PUT 2 301590 run-functional-tests-on-an-api-response-using-chai-http-iii---put-method

--description--

Lembrando que este projeto está sento construído a partir do Replit, ou pose ser clonado no GitHub.

No próximo exemplo, veremos como enviar dados em uma payload de solicitação (corpo). Vamos testar uma solicitação de PUT. O endpoint '/travellers' aceita um objeto JSON que tem essa estrutura:

{
  "surname": [last name of a traveller of the past]
}

A rota responde com:

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

Veja o código do servidor para mais detalhes.

--instructions--

Dentro de tests/2_functional-tests.js, altere o teste 'send {surname: "Colombo"}' test (// #3):

Enviar a seguinte resposta JSON como payload:

{
  "surname": "Colombo"
}

Verifique o seguinte, dentro da função de callback de request.end:

  1. status
  2. type
  3. body.name
  4. body.surname

Siga a ordem de declarações acima, pois dependemos disso. Não se esqueça de remover assert.fail() assim que o teste terminar.

--hints--

Todos os testes devem passar.

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

Você deve testar se 'res.status' será 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);
    }
  );

Você deve testar se 'res.type' será '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);
    }
  );

Você deve testar se 'res.body.name' será '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);
    }
  );

Você deve testar se 'res.body.surname' será '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.
*/