mardi 10 juillet 2018

When using the new WebApplicationFactory and resulting CreateClient, is there a way to get routes without using strings?

I have code like the following:

public class SmokeTests : IClassFixture<CustomConfigurationWebApplicationFactory<MyApplication.API.Startup>>
{
    private readonly CustomConfigurationWebApplicationFactory<MyApplication.API.Startup> _factory;

    private const string urlPrefix = "/api/";

    public SmokeTests(CustomConfigurationWebApplicationFactory<MyApplication.API.Startup> factory)
    {
        _factory = factory;
    }

    [Theory]
    [InlineData(ControllerName.Contact)] 
    [InlineData(ControllerName.Location)] // This essentially is: `public const string Contact = nameof(Contact);`
    [InlineData("LifecycleMaturitie")]
    public async Task Get_EndpointsReturn_SuccessfulCode_CorrectContentType_SomeData(string controllerName)
    {
        // Arrange
        var client = _factory.CreateClient();

        // Act
        var response = await client.GetAsync($"{urlPrefix}{controllerName}s/1");

        // Assert
        var json = await response.Content.ReadAsStringAsync();
        var dyn = JObject.Parse(json);

        response.EnsureSuccessStatusCode(); // Status Code 200-299
        Assert.Equal("application/json; charset=utf-8",
            response.Content.Headers.ContentType.ToString());
        Assert.Equal(1, dyn["Id"]);
    }
}

I know this is bad code for multiple reasons. the biggest is this nasty thing $"{urlPrefix}{controllerName}s/1" what I want to be able to do is create a route using strongly typed code like nameof. I looked into UrlHelper in core 2.1 but it requires an action context. I am assuming since the core team created all this WebApplicationFactory stuff they have also ran across this same problem.

Aucun commentaire:

Enregistrer un commentaire