apuntes:testing
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| apuntes:testing [2025/03/23 17:40] – [Test de integración] Santiago Faci | apuntes:testing [2025/03/30 19:55] (current) – Santiago Faci | ||
|---|---|---|---|
| Line 7: | Line 7: | ||
| ===== Test unitarios ===== | ===== Test unitarios ===== | ||
| + | |||
| + | ==== Clases de utilidad ==== | ||
| <code javascript> | <code javascript> | ||
| Line 41: | Line 43: | ||
| days = getDays(new Date(' | days = getDays(new Date(' | ||
| expect(days).equal(0); | expect(days).equal(0); | ||
| + | }); | ||
| + | }); | ||
| + | </ | ||
| + | |||
| + | ==== Capa controller ==== | ||
| + | |||
| + | <code javascript> | ||
| + | const httpMocks = require(' | ||
| + | const { describe, it, expect, afterEach } = require(' | ||
| + | |||
| + | jest.mock(' | ||
| + | |||
| + | const cityController = require(' | ||
| + | |||
| + | const cityService = require(' | ||
| + | const mockedFindCities = jest.spyOn(cityService, | ||
| + | const mockedRegisterCity = jest.spyOn(cityService, | ||
| + | const { mockCityArray, | ||
| + | |||
| + | afterEach(() => { | ||
| + | jest.clearAllMocks(); | ||
| + | }); | ||
| + | |||
| + | describe(' | ||
| + | it(' | ||
| + | const response = httpMocks.createResponse(); | ||
| + | const request = httpMocks.createRequest(); | ||
| + | request.app = {}; | ||
| + | request.app.conf = {}; | ||
| + | request.path = '/ | ||
| + | |||
| + | const mockedCityList = jest.fn(async () => { | ||
| + | return mockCityArray; | ||
| + | }); | ||
| + | mockedFindCities.mockImplementation(mockedCityList); | ||
| + | |||
| + | await cityController.getCities(request, | ||
| + | expect(mockedFindCities).toHaveBeenCalledTimes(1); | ||
| + | expect(response.statusCode).toEqual(200); | ||
| + | expect(response._isEndCalled()).toBeTruthy(); | ||
| + | expect(response._getJSONData().length).toEqual(5); | ||
| + | }); | ||
| + | |||
| + | it(' | ||
| + | const response = httpMocks.createResponse(); | ||
| + | const request = httpMocks.createRequest(); | ||
| + | request.app = {}; | ||
| + | request.app.conf = {}; | ||
| + | request.path = '/ | ||
| + | request.body = mockCityToRegister; | ||
| + | |||
| + | const mockedRegisterCityResponse = jest.fn(async () => { | ||
| + | return mockCityResponse; | ||
| + | }); | ||
| + | mockedRegisterCity.mockImplementation(mockedRegisterCityResponse); | ||
| + | |||
| + | await cityController.postCity(request, | ||
| + | expect(mockedRegisterCity).toHaveBeenCalledTimes(1); | ||
| + | expect(response.statusCode).toEqual(201); | ||
| + | expect(response._isEndCalled()).toBeTruthy(); | ||
| + | expect(response._getJSONData().id).toEqual(1); | ||
| + | expect(response._getJSONData().name).toEqual(' | ||
| + | expect(response._getJSONData().population).toEqual(700000); | ||
| + | expect(response._getJSONData().altitude).toEqual(200); | ||
| + | expect(response._getJSONData().age).toEqual(5); | ||
| + | expect(response._getJSONData().area).toEqual(734734); | ||
| + | expect(response._getJSONData().density).toEqual(2373); | ||
| + | expect(response._getJSONData().foundationDate).toEqual(' | ||
| }); | }); | ||
| }); | }); | ||
| Line 59: | Line 129: | ||
| ===== Test de integración ===== | ===== Test de integración ===== | ||
| - | | + | * Caso de uso: **Ok (200 OK)** |
| <code javascript> | <code javascript> | ||
| Line 96: | Line 166: | ||
| </ | </ | ||
| - | - Caso de uso: Registro (201 Created) | + | * Caso de uso: **Registro (201 Created)** |
| <code javascript> | <code javascript> | ||
| Line 124: | Line 194: | ||
| </ | </ | ||
| - | | + | * Caso de uso: **Validación (400 Bad Request)** |
| <code javascript> | <code javascript> | ||
| Line 144: | Line 214: | ||
| }); | }); | ||
| </ | </ | ||
| + | |||
| + | <code javascript> | ||
| + | . . . | ||
| + | " | ||
| + | " | ||
| + | . . . | ||
| + | . . . | ||
| + | </ | ||
| + | |||
| + | También podemos aprovechar para modificar el script '' | ||
| + | |||
| + | <code javascript> | ||
| + | . . . | ||
| + | " | ||
| + | . . . | ||
| + | " | ||
| + | . . . | ||
| + | . . . | ||
| + | </ | ||
| + | |||
| + | Asi, podremos lanzar todos los tests solo con '' | ||
| + | |||
| + | ===== Crear un entorno de pruebas con Docker ===== | ||
| + | |||
| + | Colocaremos en la carpeta '' | ||
| + | |||
| + | Podemos crear un fichero '' | ||
| + | |||
| + | <code yaml> | ||
| + | version: " | ||
| + | name: cities | ||
| + | services: | ||
| + | db: | ||
| + | image: mariadb: | ||
| + | container_name: | ||
| + | environment: | ||
| + | MYSQL_USER: ' | ||
| + | MYSQL_PASSWORD: | ||
| + | MYSQL_PORT: 3306, | ||
| + | MYSQL_ROOT_PASSWORD: | ||
| + | ports: | ||
| + | - " | ||
| + | volumes: | ||
| + | - ./ | ||
| + | </ | ||
| + | |||
| + | Y podremos lanzar nuestro entorno de pruebas con el siguiente comando: | ||
| + | |||
| + | <code bash> | ||
| + | santi@zenbook: | ||
| + | </ | ||
| + | |||
| + | Para destruir el entorno: | ||
| + | |||
| + | <code bash> | ||
| + | santi@zenbook: | ||
| + | </ | ||
| + | |||
| + | También se puede simplemente detener: | ||
| + | <code bash> | ||
| + | santi@zenbook: | ||
| + | </ | ||
| + | |||
| + | O bien iniciar si está detenido: | ||
| + | |||
| + | <code bash> | ||
| + | santi@zenbook: | ||
| + | </ | ||
| + | |||
| + | ====== Proyectos de ejemplo ====== | ||
| + | |||
| + | En [[https:// | ||
| ---- | ---- | ||
| - | (c) 2024 Santiago Faci | + | (c) 2025 Santiago Faci |
apuntes/testing.1742751610.txt.gz · Last modified: by Santiago Faci
