2016-10-12 11 views
1

asp.netコアでの統合テスト中に迷惑なエラーが発生しています。asp.netコアで統合テストを実行中に404が見つかりません

応答ステータスコードが成功を示していない:404(見つかりません)。

以下は私のコードです。

RegistrationControllerTests.cs

public class RegistrationControllerTests : IClassFixture<TestFixture<TestStartup>> 
    { 
     private readonly HttpClient _client; 

     public RegistrationControllerTests(TestFixture<TestStartup> fixture) 
     { 
      _client = fixture.Client; 
     } 

     [Fact] 
     public async void InitialiseTest() 
     { 
      //Arrange 
      var testSession = TestStartup.GetDefaultRegistrationModel(); 

      //Act 
      var response = await _client.GetAsync("/"); 
      //Assert 
      response.EnsureSuccessStatusCode(); 
      var responseString = await response.Content.ReadAsStringAsync(); 
      Assert.True(responseString.Contains(testSession.FormId)); 
     } 
    } 

TestStartup.cs

public class TestStartup 
    { 
     public void ConfigureServices(IServiceCollection services) 
     { 
      services.AddMvc(); 
      services.AddSingleton(provider => Configuration); 
      services.AddTransient<IRegistrationRepository, ServiceUtilities>(); 
      services.AddTransient<IClientServiceConnector, ClientServiceValidation>(); 
      services.AddTransient<IEmailSender, EmailSender>(); 
     } 

     private IConfiguration Configuration { get; set; } 
     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
     public void Configure(IApplicationBuilder app, IHostingEnvironment enviroment) 
     { 
      app.UseStaticFiles(); 
      app.UseFileServer(); 
      ConfigureRestAuthenticationSetting(enviroment); 
      app.UseMvc(ConfigureRoutes); 

      //app.Run(async (context) => 
      //{ 
      // await context.Response.WriteAsync("Hello World!"); 
      //}); 
     } 

     private void ConfigureRestAuthenticationSetting(IHostingEnvironment enviroment) 
     { 
      var config = new ConfigurationBuilder() 
       .SetBasePath(Path.Combine(enviroment.ContentRootPath,"wwwroot")) 
       .AddJsonFile("TestConfig.json"); 
      Configuration = config.Build(); 
     } 

     private void ConfigureRoutes(Microsoft.AspNetCore.Routing.IRouteBuilder routeBuilder) 
     { 
      routeBuilder.MapRoute("Default", "{controller=Registration}/{action=Index}/{formId}"); 
     } 

     public static RegistrationModel GetDefaultRegistrationModel() 
     { 
      return new RegistrationModel 
      { 
       FormId = "12345" 
      }; 
     } 
    } 

TestFixture.cs

public class TestFixture<TStartup> : IDisposable 
    { 
     private const string SolutionName = "RegistrationApplication.sln"; 
     private readonly TestServer _server; 
     public HttpClient Client { get; } 

     public TestFixture() 
      :this(Path.Combine("src")) 
     { 

     } 
     protected TestFixture(string solutionRelativeTargetProjectParentDir) 
     { 
      var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; 
      var contentRoot = GetProjectPath(solutionRelativeTargetProjectParentDir, startupAssembly); 

      var builder = new WebHostBuilder() 
       .UseContentRoot(contentRoot) 
       .ConfigureServices(InitializeServices) 
       .UseEnvironment("Development") 
       .UseStartup(typeof(TStartup)); 


      _server = new TestServer(builder); 

      Client = _server.CreateClient(); 
      Client.BaseAddress = new Uri("https://localhost:44316/Registration"); 
     } 

     protected virtual void InitializeServices(IServiceCollection services) 
     { 
      var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly; 
      var manager = new ApplicationPartManager(); 
      manager.ApplicationParts.Add(new AssemblyPart(startupAssembly)); 

      manager.FeatureProviders.Add(new ControllerFeatureProvider()); 
      manager.FeatureProviders.Add(new ViewComponentFeatureProvider()); 
      services.AddSingleton(manager); 

     } 
     private static string GetProjectPath(string solutionRelativePath, Assembly startupAssembly) 
     { 
      var projectName = "RegistrationApplication"; 
      var applicationBasePath = PlatformServices.Default.Application.ApplicationBasePath; 
      var directoryInfo = new DirectoryInfo(applicationBasePath); 
      do 
      { 
       var solutionFileInfo = new FileInfo(Path.Combine(directoryInfo.FullName, SolutionName)); 
       if (solutionFileInfo.Exists) 
       { 
        return Path.GetFullPath(Path.Combine(directoryInfo.FullName, solutionRelativePath, projectName)); 
       } 
       directoryInfo = directoryInfo.Parent; 
      } 
      while (directoryInfo.Parent != null); 
      throw new NotImplementedException($"Solution root could not be located using application root {applicationBasePath}."); 
     } 

     public void Dispose() 
     { 
      Client.Dispose(); 
      _server.Dispose(); 
     } 
    } 

答えて

0

GITでTestConfig.jsonをコミットする必要があるという問題があります。それはそれが見つからなかったので、それは404エラーを投げていた理由です。 TestConfig.jsonファイルをコミットした後、すべてが魅力のように機能します。

以下は、TestConfig.jsonが使用されているコードです。

private void ConfigureRestAuthenticationSetting(IHostingEnvironment enviroment) 
     { 
      var config = new ConfigurationBuilder() 
       .SetBasePath(Path.Combine(enviroment.ContentRootPath,"wwwroot")) 
       .AddJsonFile("TestConfig.json"); 
      Configuration = config.Build(); 
     } 
関連する問題