2016-06-08 60 views
0

相关但不作为this RC1到RC2集成测试的问题,我想弄清楚发生了什么事UseServicesAsp.net核心RC1到RC2集成测试: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(); 

我的样品中的TestApplicationEnvironment上面似乎是一个最小的模拟对象,这样相同的:

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); 
     } 
    } 

对此没有文档(docs.asp.net尚未更新)。有谁知道如何编译此代码?上面显示的缺少的代码注释掉了会设置一些应用程序级别的环境设置并将其作为应用程序环境提供。到目前为止,它似乎已经消失了,可能会被IHostingEnvironment所取代,但如何为集成测试提供嘲讽的IHostingEnvironment当然不是很清楚,事物的解耦性质可能由一些不存在的mixin提供在我的project.json上,所以我不知道如何继续。

可能这个代码不再需要测试通过,如果我找出解决方案,我会自己回答。

在与上面的代码中注释掉运行时,我得到的指示为代码所需的东西继续缺失,可能与不能够解决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... 
+0

这个中国文件可以有什么用处吗? http://dlcenter.gotop.com.tw/SampleFiles/ACL041300/%E8%A3%9C%E5%85%85%E5%8F%8A%E6%9B%B4%E6%96%B0%E8%B3 %87%E6%96%99/B.PDF – silkfire

+0

甚至没有接近。这是2015年中期“beta4”时代的古老东西,我正在问RC2。我也不会说中文。 –

+0

好的,对不起,我的坏:/ – silkfire

回答

1

我认为你正在寻找ConfigureServices

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

这似乎是正确的API。修复我的测试似乎比这更深入,但我会将此标记为已接受,并且如果我找出在将假IApplicationEnvironment(RC1)转换为假或模拟时应完成的工作,我将编辑我的答案或夹具IHostingEnvironment(RC2)。 –