Hi,
I create bellow custom factory for E2E test:
public class CustomWebApplicationFactory : WebApplicationFactory<Program> { protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureServices(services => { // extend service collection }); builder.ConfigureTestServices(services => { // extend service collection }); builder.Configure(app => { // extend application builder }); } }
Also, this is my Program file:
using System.Reflection; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers() .AddApplicationPart(Assembly.GetExecutingAssembly());; // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.MapGet("WeatherForecast", _ => { var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), TemperatureC = Random.Shared.Next(-20, 55), Summary = summaries[Random.Shared.Next(summaries.Length)] })); }); app.Run(); public partial class Program{}
When I want to call WeatherForecast
GET endpoint, with WebApplicationFactory Client
I get NotFound
response from client in my test instead of OK
public class UnitTest1: CustomWebApplicationFactory { [Fact] public async Task Test1() { var response = await CreateClient().GetAsync("/WeatherForecast"); response.Should().Be200Ok(); } }
If I remove below section from My CustomFactory my http client returns OK status code
builder.Configure(app => { // extend application builder });Expected Behavior
I Expect to get a 200 ok status code for this http call on http client
Steps To ReproduceRun this test
Exceptions (if any)Xunit.Sdk.XunitException Expected response to be HttpStatusCode.OK {value: 200}, but found HttpStatusCode.NotFound {value: 404}. The HTTP response was: HTTP/1.1 404 NotFound Content-Length: 0 The originated HTTP request was: GET http://localhost/WeatherForecast HTTP 1.1 Content-Length: 0 at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message) at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message) at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message) at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc) at FluentAssertions.Execution.AssertionScope.FailWith(Func`1 failReasonFunc) at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args) at FluentAssertions.Web.HttpResponseMessageAssertions.Be200Ok(String because, Object[] becauseArgs) at FluentAssertions.HttpStatusCodeAssertionsExtensions.Be200Ok(HttpResponseMessageAssertions`1 parent, String because, Object[] becauseArgs) at E2E.UnitTest1.Test1() in C:\Users\MehdiHadeli\Desktop\E2E\Test\UnitTest1.cs:line 11 at Xunit.Sdk.TestInvoker`1.<>c__DisplayClass48_0.<<InvokeTestMethodAsync>b__1>d.MoveNext() in /_/src/xunit.execution/Sdk/Frameworks/Runners/TestInvoker.cs:line 264 --- End of stack trace from previous location --- at Xunit.Sdk.ExecutionTimer.AggregateAsync(Func`1 asyncAction) in /_/src/xunit.execution/Sdk/Frameworks/ExecutionTimer.cs:line 48 at Xunit.Sdk.ExceptionAggregator.RunAsync(Func`1 code) in /_/src/xunit.core/Sdk/ExceptionAggregator.cs:line 90.NET Version
7.0.0
Anything else?No response
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4