2

I am trying to write integration tests for my web service.

I want to separate Program.cs into Startup.cs so test class can mock TestStartup.cs(or something like so). The point is swapping real services with mock services.

Attempt to use Startup.cs file for MinimalApi resulting app not compilable:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) {
    (...)
    // Error CS1929  'IApplicationBuilder' does not contain a definition for
    // 'MapGroup' and the best extension method overload 'EndpointRouteBuilderExtensions.MapGroup(IEndpointRouteBuilder, string)'
    // requires a receiver of type 'Microsoft.AspNetCore.Routing.IEndpointRouteBuilder'
    app.MapGroup("/product").MapProductApi().WithTags("Product");
    app.UseResponseCaching();
    app.UseOutputCache();
}

Am I going the right direction? Should Minimal Apis be used with Startup.cs?

1 Answer 1

3

You can reconfigure the services with the usual test setup (see this answer for more details):

var application = new WebApplicationFactory<Program>()
    .WithWebHostBuilder(builder =>
    {
        builder.ConfigureServices(services =>
        {
            // set up servises
            services.RemoveAll(typeof(RegisteredServiceTypeToReplace));
            // add new registration for RegisteredServiceTypeToReplace
        });
    });

As for :

// Error CS1929 'IApplicationBuilder' does not contain a definition for 'MapGroup'...

You can try using UseEndpoints:

app.UseEndpoints(routeBuilder =>
{
    routeBuilder.MapGroup(...);
    // ...
})
Sign up to request clarification or add additional context in comments.

4 Comments

What if we we don't want to rely on ASPNet.MVC library? is there another class to inject Program into it?
@MarwaAhmad "What if we we don't want to rely on ASPNet.MVC library" - can you please elaborate?
Guru Stron: The WebApplicationFactory exists in ASPNET.MVC library, and we don't want to use it, we want to use what exist in ASPNetCore and Azure.Function only.
@MarwaAhmad still not sure why. The class I've used comes from Microsoft.AspNetCore.Mvc.Testing, not ASPNET.MVC library. And I don't see any issues using it in test project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.