[英]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??
这是测试失败的地方,在设置身份验证后(选中此),我无法到达 controller 端点本身,我猜主机没有正确构建? 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.
但如果是这样的话,那么除了 404 之外,我应该得到一些回应。我只是被重定向到处理资源,就是这样。 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());
}
public class AccountControllerEndTest: IClassFixture<WebApplicationFactory> { private readonly WebApplicationFactory factory;
公共 class AccountControllerEndTest:IClassFixture<WebApplicationFactory> { private readonly WebApplicationFactory 工厂;
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.
这就是我构建主机的方式,class fixture 用你在我给定的配置文件中提供的配置装饰主机,我必须先更改目录,因为测试项目没有从根目录中找到 json同行。 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.
问题是 Program.cs 在创建构建器时应该接受一组参数,最初对我来说是 null,而主机没有构建,因为夹具 class 无法向主机指示配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.