Amardeep Singh
Amardeep Singh

Reputation: 11

Integration Tests .NET 6 - 404 not Found

I was trying to write integeration tests using the same set of actions that I have earlier used in other microservices on my project.

Creating a Client using a specific set of environment variables

Adding authentication headers

This is where the test fails, after auth is set (checked this) , I am not able to reach the controller endpoint itself, I'm guessing the host is not building properly?? But if that is the case, then I should be getting something in response apart from 404. I just get redirected to dispose off resources and that's it. I haven't been able to figure this out, again, this has been used on multiple occasions in a number of tests which are fine.

Can someone point out where to exactly look for or have experienced something like this maybe????

    public async Task Users(string url)
    {
        //Act
        var http = factory.CreateClient();
        var request = new HttpRequestMessage(HttpMethod.Get, $"/api/v1/Account/{url}");
      
        await request.AddWamHeaders();
        var response = await http.SendAsync(request);
        Console.WriteLine(response.Content.ReadAsStreamAsync());
        //Assert
        response.EnsureSuccessStatusCode();
        JObject data = JsonConvert.DeserializeObject<JObject>(await response.Content.ReadAsStringAsync());
        Assert.True(data["Data"].Count() > 1);
        Assert.Equal("application/json; charset=utf-8",
        response.Content.Headers.ContentType.ToString());
    }

Upvotes: 0

Views: 541

Answers (1)

Amardeep Singh
Amardeep Singh

Reputation: 11

public class AccountControllerEndTest : IClassFixture<WebApplicationFactory> { private readonly WebApplicationFactory factory;

    public AccountControllerEndTest(WebApplicationFactory<Program> factory)
    {
        Environment.SetEnvironmentVariable("ASSET_GROUP", "test");
        this.factory = factory.WithWebHostBuilder(builder =>
        {
            builder.UseContentRoot(Directory.GetCurrentDirectory());
            builder.UseEnvironment("test");
            builder.UseConfiguration(new ConfigurationBuilder().AddJsonFile("appsettings.test.json")
                .Build());
            Console.WriteLine(builder.ToString());
        });
    }

This is how I was building the Host, the class fixture decorates the host with the config you set as provided in my given config file, I had to change the directory first, because the test project does not locate the json from the root of the peer . The issue was that the Program.cs should be accepting a set of args while creating a builder , it was null for me originally, and the host was not building because the fixture class was not able to dictate the configurations to the host.

Upvotes: 1

Related Questions