0% found this document useful (0 votes)
31 views

Using Xunit

The document contains code to test the GET endpoint for retrieving airplane data. It sets up a test database, inserts sample airplane data, makes a request to the GET /Avion endpoint, and asserts that the response has a success status code.

Uploaded by

Rousse Rub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Using Xunit

The document contains code to test the GET endpoint for retrieving airplane data. It sets up a test database, inserts sample airplane data, makes a request to the GET /Avion endpoint, and asserts that the response has a success status code.

Uploaded by

Rousse Rub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using Xunit;

using System.Net.Http;
using System.Net.Http.Json;
using Microsoft.AspNetCore.Mvc.Testing;
using BackendAerolinea.Models;
using BackendAerolinea;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

using System.Net;

namespace TestingAvion;

public class AvionTest1 : IClassFixture<WebApplicationFactory<Program>>


{

[Fact]
public async Task getAvion_ShouldReturnExistingAirplane()
{
// Arrange
var builder = WebApplication.CreateBuilder(new string[0]);
var connectionString = "Server=DESKTOP-9A9H02P\\
SQLEXPRESS;Database=Aerolineadb;Trusted_Connection=true;TrustServerCertificate=true
";

builder.Services.AddDbContext<DataContext>(options =>
{
builder.Services.AddDbContext<DataContext>(options =>
{
options.UseSqlServer(connectionString); // Configura la conexión a
SQL Server
});
});
var app = builder.Build();
var context = app.Services.GetRequiredService<DataContext>();
await context.Database.EnsureCreatedAsync();
var client = app.Services.GetRequiredService<HttpClient>(); // Obtén un
HttpClient para realizar solicitudes.

var avionesData = new List<Avion>


{
new Avion
{
Id = 1,
NombreAvion = "Boeing 747",
Horasalida = TimeSpan.FromHours(8),
Horallegada = TimeSpan.FromHours(16),
Aeropuertosalida = "Aeropuerto A",
Aeropuertollegada = "Aeropuerto B",
StatusVuelo = "En vuelo",
PasajerosLimite = 300,
LimitePeso = 20000
},
new Avion
{
Id = 2,
NombreAvion = "Airbus A380",
Horasalida = TimeSpan.FromHours(12),
Horallegada = TimeSpan.FromHours(20),
Aeropuertosalida = "Aeropuerto X",
Aeropuertollegada = "Aeropuerto Y",
StatusVuelo = "En tierra",
PasajerosLimite = 400,
LimitePeso = 22000
}
};
context.Avion.AddRange(avionesData);
await context.SaveChangesAsync();

//Act
var response = await client.GetAsync("/Avion");

// Assert
response.EnsureSuccessStatusCode();
}
}

_________________________________________________________________________

using Xunit;
using Microsoft.AspNetCore.Mvc.Testing;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using BackendAerolinea;

namespace BackendAerolinea.Tests
{
public class AvionTests : IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;

public AvionTests()
{

[Fact]
public async Task TestGetAvionEndpoint()
{
// Arrange
var client = _factory.CreateClient();

// Act
var response = await client.GetAsync("/Avion");

// Assert
response.EnsureSuccessStatusCode();
// Add more assertions based on the expected behavior
}

// Add more tests for other endpoints


}
}

You might also like