Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Using Xunit

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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