简体   繁体   中英

Asp.net core RC1 to RC2 integration testing: where did UseServices go?

Related but not the same as this RC1 to RC2 integration test question, I am trying to figure out what happened to UseServices

//RC1: 
// TestServer = new TestServer(TestServer
//                .CreateBuilder()
//                 .UseStartup<Startup>());
//RC2: 
// TestServer=new TestServer(new WebHostBuilder().UseEnvironment....)

var builder2 = new WebHostBuilder()
 .UseEnvironment("Development")
/* RC2 port:
.UseServices(services =>
{
                // Change the application environment to the mvc project
                var env = new                TestApplicationEnvironment();
    env.ApplicationBasePath = Path.GetFullPath(Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "..", "..", "src", "RamSoft.Fhir.Api"));
    env.ApplicationName = "RamSoft Patient Api Service";

    services.AddInstance<IApplicationEnvironment>(env);
})
*/
.UseStartup<RamSoft.Fhir.Api.Startup>();
TestServer = new TestServer(builder2);
Client = TestServer.CreateClient();

The TestApplicationEnvironment in my sample above appears to be a minimal mock object, like this:

 public class TestApplicationEnvironment : IApplicationEnvironment
    {
        public string ApplicationBasePath { get; set; }

        public string ApplicationName { get; set; }

        public string ApplicationVersion => PlatformServices.Default.Application.ApplicationVersion;

        public string Configuration => PlatformServices.Default.Application.Configuration;

        public FrameworkName RuntimeFramework => PlatformServices.Default.Application.RuntimeFramework;

        public object GetData(string name)
        {
            return PlatformServices.Default.Application.GetData(name);
        }

        public void SetData(string name, object value)
        {
            PlatformServices.Default.Application.SetData(name, value);
        }
    }

There is no documentation (docs.asp.net has not been updated yet) on this. Does anyone know how to make this code compile? The missing code shown above commented out would have set up some application level environment settings and provided it as an Application Environment. So far it seems this is gone, and may be replaced by IHostingEnvironment, but how to provide the mocked IHostingEnvironment to an integration test is of course not clear, with the decoupled nature of things, this may be provided by some mixin that is not present on my project.json, and so I have no idea how to proceed.

It may be that this code is no longer necessary for the test to pass, and if I figure out the solution, I will answer this myself.

At runtime with the code above commented out, I get an error that indicates that something required for the code to continue is missing, probably an issue with dependency inversion container not being able to resolve IConfiguration:

System.Exception was unhandled by user code
  HResult=-2146233088
  Message=Could not resolve a service of type 'Microsoft.Extensions.Configuration.IConfiguration' for the parameter 'configuration' of method 'Configure' on type 'X.Y.Api.Startup'.
  Source=Microsoft.AspNetCore.Hosting
  StackTrace:
       at Microsoft.AspNetCore.Hosting.Startup.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
       at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
       at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
       at Microsoft.AspNetCore.TestHost.TestServer..ctor(IWebHostBuilder builder)
       at X.Y.IntegrationTests.TestServerFixture..ctor() in D:\....IntegrationTests\TestServerFixture.cs:line 68
  InnerException...

I think you're looking for ConfigureServices :

.ConfigureServices(services => {
    //Make your `env`...
    services.AddSingleton(env);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM