You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

2 D铆as
2 Hrs
43 Min
15 Seg

Pruebas en conjunto: API y Base de Datos

15/17
Resources

How to validate that an API aligns with the database?

When developing software, the alignment between APIs and databases is crucial to maintain the integrity of the system. Throughout this class, we have learned how to perform tests that ensure that what an API returns is consistent with the information in the database. Imagine you have a payment system: the API must return amounts that match those stored in the database and in the proper format, such as adding a currency sign. This type of testing is essential to ensure that the software can be trusted.

How to implement joining API and database tests?

The class focuses on uniting knowledge of API and database testing. We will perform a step-by-step exercise on how to delete a record in the database using an API to validate its operation.

Creating the test environment

Before proceeding, it is essential to make sure that the server is running. If it is not, you can start it with the command configured in the package.json file. Now, let's create a new test called 'joining all'.

describe('joining all', () => { it('should delete a record', () => { // Request to delete a specific record cy.request({ url: '/employees/3', // ID of the record to delete method: 'DELETE' }).then((response) => { // Validate the status of the response expect(response.status).to.equal(200); });
 // Validate that the record is no longer in the database cy.task('queryDb', 'SELECT * FROM employees WHERE id = 3').then((results) => { // Verify that there are no records with that ID expect(results.length).to.equal(0); }); }); }); });

Using plugins for queries

Once the entry has been deleted using the API, it is crucial to check the database to confirm the deletion. This is where custom plugins come in handy, such as the queryDb plugin example.

Creating dynamic requests

Flexibility is key when creating tests. You can use string interpolation to handle IDs dynamically, avoiding static code in test environments:

const employeeId = 3; // Or whatever ID you need
cy.request({ url: `/employees/${employeeId}`, method: 'DELETE'}).then(response => { expect(response.status).to.equal(200);});

What challenges should you explore?

Implementing what you have learned in more complex scenarios is an effective way to consolidate your skills. Here are some challenges:

  • Create a record in the database and store its ID in the context using an alias.
  • Test the consistency between what the API returns and what you stored in the database.
  • Develop methods that work for non-relational databases.
  • Expand these examples to more complicated scenarios and share your learnings and experiences, encouraging knowledge sharing among developers.

Proper integration and validation between APIs and databases is essential for the development of robust and reliable software. Keep learning and sharing your knowledge!

Contributions 1

Questions 2

Sort by:

Want to see more contributions, questions and answers from the community?

隆Super! 馃
.
En definitiva, Cypress nos otorga una automatizaci贸n de pruebas con un alcance o l铆mite del frontend que, en el backend son diferentes en variables globales window, por ejemplo.
.
Especializando elementos en capas m谩s profundas con una interacci贸n en base de datos, mediante mocks, se ve demasiado condenado el uso de donde, para ser honesto, se requiere que el servidor est茅 levantado. Para estos casos, utilizamos supertest, el cual nos abstrae las pruebas http mediante un agente que encapsula nuestro server.
.
Por dem谩s, me quedo con Cypress en el frontend donde, en el caso de backend, seguir茅 estos recursos para probar la aplicaci贸n.