1

我試圖創建一個集成測試項目,用於測試我的網絡api .Net核心與微軟樣本這裏的最佳做法https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing 但我不知道爲什麼,我總是得到一個錯誤消息[404找不到], HttpClient找不到'testServer'。而且我確定我的'路由'是正確的[「api/Registration /」+ guid],如果我嘗試iisexpress的調試模式。它的工作很好測試集成.Net Core與TestServer返回404找不到?

如果有人有一些想法來解決它,將是非常gratefull。由於

這是我在整合測試項目中使用的代碼:

RegistrationIntegrationTest.cs:

[TestClass] 
public class RegistrationIntegrationTest 
{ 
    private static TestServer server; 
    private static HttpClient client; 

    [ClassInitialize] 
    public static void ClassInitialize(TestContext context) 
    { 
     var basePath = PlatformServices.Default.Application.ApplicationBasePath; 
     var projectPath = Path.GetFullPath(Path.Combine(basePath, "../../../../Registration")); 

     var builder = new WebHostBuilder() 
      .UseContentRoot(projectPath) 
      .UseEnvironment(EnvironmentName.Development) 
      .UseStartup<Startup>(); 

     server = new TestServer(builder); 
     client = server.CreateClient(); 
    } 

    private async Task<string> GetRegistrationResponse(string guid) 
    { 
     var request = "api/Registration/" + guid; 

     var response = await client.GetAsync(request); 
     response.EnsureSuccessStatusCode(); 

     return await response.Content.ReadAsStringAsync(); 
    } 

    [TestMethod] 
    public async Task GetRegistration() 
    { 
     // Act 
     var responseString = await GetRegistrationResponse("A5A3CBFD-8B61-E711-80E2-00505693113A"); 
     // Assert 
     Assert.Equals("test", 
      responseString); 
    } 
} 

這裏是我的啓動類從我的集成測試項目:

Startup.cs

public Startup(IHostingEnvironment env) 
    { 
     var builder = new ConfigurationBuilder() 
      .SetBasePath(env.ContentRootPath) 
      //.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 
      //.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
      .AddEnvironmentVariables(); 
     Configuration = builder.Build(); 

    } 

    public IConfigurationRoot Configuration { get; } 

    // This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddMvc(); 
     services.AddCors(); 
     services.AddAntiforgery(); 
     services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(this.Container)); 
     services.Configure<MyAppSettings>(Configuration); 
     services.UseSimpleInjectorAspNetRequestScoping(this.Container); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(Configuration.GetSection("Logging")); 

     if (env.IsDevelopment()) 
     { 
      loggerFactory.AddDebug(); 
      app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); 
     } 
     else if (env.IsStaging()) 
     { 
      app.UseCors(builder => builder.WithOrigins("https://apitest.swisscaution.ch")); 
     } 
     else if (env.IsProduction()) 
     { 
      app.UseCors(builder => builder.WithOrigins("https://internalapi.swisscaution.ch")); 
     } 
     else 
     { 
      throw new InvalidOperationException("Bad application environement."); 
     } 

     app.UseMvc(); 

     this.InitializeContainer(app); 
    } 

這裏是我的解決方案Visual Studio中, 正如你所看到的建築的照片,集成測試項目是從所謂的「註冊」 enter image description here

的例外,我讓我的網絡API項目是分開的: enter image description here

回答

1

我終於發現問題了。 是因爲我的[集成測試項目]的目標是.Net核心版本,我試圖用.Net Framework 4.5.6版本測試和託管一個項目。 我在網上找到解決方案,我需要修改csproj文件並在「PropertyGroup」部分添加這3個配置。

<PropertyGroup> 
    <TargetFramework>net461</TargetFramework> 
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> 
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> 
    </PropertyGroup> 

這是我找到的網址:http://quabr.com/44027215/system-net-http-httpclient-microsoft-aspnetcore-testhost-testserver-createclient