Mvc: WebApplicationFactory calls Startup.ConfigureServices after ConfigureWebHost blowing away testing configuration.

Created on 13 Aug 2018  路  3Comments  路  Source: aspnet/Mvc

Is this a Bug or Feature request?:

Either bug or I'm not using as intended.

Steps to reproduce (preferably a link to a GitHub repo with a repro project):

  1. Create subclass of WebApplicationFactory
  2. Override WebApplicationFactory.ConfigureWebHost and configure testing specific services
  3. Debug tests with breakpoints in TStartup.ConfigureServices

Description of the problem:

I'm using a custom subclass of WebApplicationFactory in order to wire up testing services (namely, a allow any authentication scheme and custom user service) however, after the testing services are configured the base startup classes configure method is called, effectively blowing away my test services
and using real authentication and a real user service.

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All:

Microsoft.AspNetCore.App 2.1.2

It seems intuitive to me that the startup classes configuration should be executed first, and the custom web application's factory then has the option to override the applications production configuration, but if I'm using this incorrectly, then I'd like some guidance on how to do this.

I tried using an testing specific startup class, but then Mvc couldn't find my controllers and all routes returned 404.

right now I have an ugly hack where the integration test sets the environment to "Testing" and the startup class ConfigureServices method returns immediately if environment is "Testing"

Would really like to avoid adding any testing code to my main application project.

investigate

Most helpful comment

@CzechsMix Thanks for reaching out.

ConfigureServices always gets called before Startup.ConfigureServices. I imagine that what you are doing is configuring some options type, for that. You can use ConfigureTestServices which will run after Startup.ConfigureServices.

Beware that there are some limitations on where ConfigureTestServices can be used, but as long as your Startup.ConfigureServices method has the following signature void ConfigureServices(IServiceCollection services) you should be fine.

Let us know if this doesn't solve your issue.

Closing this issue as there's no more action to be taken here.

All 3 comments

Thanks for contacting us, @CzechsMix.
@javiercn. can you please look into this? Thanks!

@CzechsMix Thanks for reaching out.

ConfigureServices always gets called before Startup.ConfigureServices. I imagine that what you are doing is configuring some options type, for that. You can use ConfigureTestServices which will run after Startup.ConfigureServices.

Beware that there are some limitations on where ConfigureTestServices can be used, but as long as your Startup.ConfigureServices method has the following signature void ConfigureServices(IServiceCollection services) you should be fine.

Let us know if this doesn't solve your issue.

Closing this issue as there's no more action to be taken here.

That appears to be exactly what I'm looking for, thank you!

Was this page helpful?
0 / 5 - 0 ratings