Skip to content

Commit 8d46aa7

Browse files
Merge pull request #24 from platzi/amines/3
Método de obtener contactos agregado
2 parents 50c3a2e + 5a02a60 commit 8d46aa7

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Net;
2+
using System.Text.Json;
3+
using Microsoft.AspNetCore.Mvc.Testing;
4+
using Xunit;
5+
6+
public class ContactosTest : IClassFixture<WebApplicationFactory<Program>>
7+
{
8+
private readonly WebApplicationFactory<Program> _factory;
9+
10+
public ContactosTest(WebApplicationFactory<Program> factory)
11+
{
12+
_factory = factory;
13+
}
14+
15+
[Fact]
16+
public async Task ObtenerContactos_ReturnsExpectedContactList()
17+
{
18+
// Arrange
19+
var client = _factory.CreateClient();
20+
21+
// Act
22+
var response = await client.GetAsync("/obtenercontactos");
23+
24+
// Assert
25+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
26+
27+
var responseString = await response.Content.ReadAsStringAsync();
28+
var contactos = JsonSerializer.Deserialize<List<string>>(responseString, new JsonSerializerOptions
29+
{
30+
PropertyNameCaseInsensitive = true
31+
});
32+
33+
Assert.NotNull(contactos);
34+
Assert.Equal(3, contactos.Count);
35+
Assert.Contains("Amin Espinoza", contactos);
36+
Assert.Contains("Oscar Barajas", contactos);
37+
Assert.Contains("Pepe Rodelo", contactos);
38+
}
39+
}

src/ContactosAPI/Program.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@
5959
.WithName("GetWeatherForecast")
6060
.WithOpenApi();
6161

62+
app.MapGet("/obtenercontactos", () =>
63+
{
64+
var contactos = new List<string> { "Amin Espinoza", "Oscar Barajas", "Pepe Rodelo" };
65+
return contactos;
66+
})
67+
.WithName("ObtenerContactos")
68+
.WithOpenApi();
69+
70+
app.Run();
71+
6272
app.Run();
6373

6474
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)

0 commit comments

Comments
 (0)