Entornos de desarrollo

1º DAM/DAW - Curso 2024-2025

User Tools

Site Tools


apuntes:testing

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
apuntes:testing [2025/03/23 18:04] – [Crear un entorno de pruebas con Docker] Santiago Faciapuntes: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('2024-01-01'), new Date('2024-01-01'));         days = getDays(new Date('2024-01-01'), new Date('2024-01-01'));
         expect(days).equal(0);         expect(days).equal(0);
 +    });
 +});
 +</code>
 +
 +==== Capa controller ====
 +
 +<code javascript>
 +const httpMocks = require('node-mocks-http');
 +const { describe, it, expect, afterEach } = require('@jest/globals');
 +
 +jest.mock('../../service/cities');
 +
 +const cityController = require('../../controller/cities');
 +
 +const cityService = require('../../service/cities');
 +const mockedFindCities = jest.spyOn(cityService, 'findCities');
 +const mockedRegisterCity = jest.spyOn(cityService, "registerCity");
 +const { mockCityArray, mockCityToPost, mockCityResponse, mockCityToRegister } = require('./mocks/cities');
 +
 +afterEach(() => {
 +    jest.clearAllMocks();
 +});
 +
 +describe('cities', () => {
 +    it('GET /cities should get a city list', async () => {
 +        const response = httpMocks.createResponse();
 +        const request = httpMocks.createRequest();
 +        request.app = {};
 +        request.app.conf = {};
 +        request.path = '/cities';
 +
 +        const mockedCityList = jest.fn(async () => {
 +            return mockCityArray;
 +        });
 +        mockedFindCities.mockImplementation(mockedCityList);
 +
 +        await cityController.getCities(request, response);
 +        expect(mockedFindCities).toHaveBeenCalledTimes(1);
 +        expect(response.statusCode).toEqual(200);
 +        expect(response._isEndCalled()).toBeTruthy();
 +        expect(response._getJSONData().length).toEqual(5);
 +    });
 +
 +    it('POST /cities should register a new city', async () => {
 +        const response = httpMocks.createResponse();
 +        const request = httpMocks.createRequest();
 +        request.app = {};
 +        request.app.conf = {};
 +        request.path = '/cities';
 +        request.body = mockCityToRegister;
 +
 +        const mockedRegisterCityResponse = jest.fn(async () => {
 +            return mockCityResponse;
 +        });
 +        mockedRegisterCity.mockImplementation(mockedRegisterCityResponse);
 +
 +        await cityController.postCity(request, response);
 +        expect(mockedRegisterCity).toHaveBeenCalledTimes(1);
 +        expect(response.statusCode).toEqual(201);
 +        expect(response._isEndCalled()).toBeTruthy();
 +        expect(response._getJSONData().id).toEqual(1);
 +        expect(response._getJSONData().name).toEqual('Zaragoza');
 +        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('2020-12-01');
     });     });
 }); });
Line 59: Line 129:
 ===== Test de integración ===== ===== Test de integración =====
  
- Caso de uso: Ok (200 OK)+  * Caso de uso: **Ok (200 OK)**
  
 <code javascript> <code javascript>
Line 96: Line 166:
 </code> </code>
  
-Caso de uso: Registro (201 Created)+  * Caso de uso: **Registro (201 Created)**
  
 <code javascript> <code javascript>
Line 124: Line 194:
 </code> </code>
  
- Caso de uso: Validación (400 Bad Request)+  * Caso de uso: **Validación (400 Bad Request)**
  
 <code javascript> <code javascript>
Line 144: Line 214:
 }); });
 </code> </code>
 +
 +<code javascript>
 +. . .
 +"scripts": {
 +    "integration-test": "mocha src/test/integration --exit",
 +    . . .
 +. . .
 +</code>
 +
 +También podemos aprovechar para modificar el script ''test'' de forma que lance todos los tests: unitarios y de integración:
 +
 +<code javascript>
 +. . .
 +"scripts": {
 +    . . .
 +    "test": "npm run unit-test && npm run integration-test",
 +    . . . 
 +. . .
 +</code>
 +
 +Asi, podremos lanzar todos los tests solo con ''npm test''
  
 ===== Crear un entorno de pruebas con Docker ===== ===== Crear un entorno de pruebas con Docker =====
Line 172: Line 263:
  
 <code bash> <code bash>
-santi@zenbook:$ docker compose -f docker.compose-dev.yaml up -d+santi@zenbook:$ docker compose -f docker.compose.dev.yaml up -d
 </code> </code>
  
Line 178: Line 269:
  
 <code bash> <code bash>
-santi@zenbook:$ docker compose -f docker.compose-dev.yaml down+santi@zenbook:$ docker compose -f docker.compose.dev.yaml down
 </code> </code>
  
 También se puede simplemente detener: También se puede simplemente detener:
 <code bash> <code bash>
-santi@zenbook:$ docker compose -f docker.compose-dev.yaml stop+santi@zenbook:$ docker compose -f docker.compose.dev.yaml stop
 </code> </code>
  
Line 189: Line 280:
  
 <code bash> <code bash>
-santi@zenbook:$ docker compose -f docker.compose-dev.yaml start+santi@zenbook:$ docker compose -f docker.compose.dev.yaml start
 </code> </code>
  
apuntes/testing.1742753098.txt.gz · Last modified: 2025/03/23 18:04 by Santiago Faci