2016-06-08 62 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)。 –