v1.31.0
conocimientotesting-patternstemplates/1204 B

ApiTestBase.cs.template

Fichero que /hv:testing-patterns usa o materializa en el proyecto. Se muestra tal cual está en el plugin.

testing-patternsskill
templatescarpeta
1204 Btamaño
2026-10-06último cambio del pack
// TEMPLATE: ApiTestBase | Skill: testing-patterns
// Base for API tests with WebApplicationFactory
// Placeholders: {{ProjectNamespace}}

using System.Net.Http.Headers;
using System.Net.Http.Json;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace {{ProjectNamespace}}.Tests.Api;

public abstract class ApiTestBase : IClassFixture<WebApplicationFactory<Program>>
{
    protected readonly HttpClient Client;

    protected ApiTestBase(WebApplicationFactory<Program> factory)
    {
        Client = factory.WithWebHostBuilder(builder =>
        {
            builder.ConfigureServices(services =>
            {
                // Override DB, auth, etc. para tests
            });
        }).CreateClient();

        Client.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Bearer", GenerateTestToken());
    }

    private static string GenerateTestToken() => "test-token";

    protected async Task<T?> GetAsync<T>(string url)
        => await Client.GetFromJsonAsync<T>(url);

    protected async Task<HttpResponseMessage> PostAsync<T>(string url, T body)
        => await Client.PostAsJsonAsync(url, body);
}

← Volver a la ficha de testing-patterns