2016-06-15 16 views
4

ASP.Net Core RC2で動作する統合テストを行うのに苦労しています。 ブラウザで正常に動作する基本的なWebプロジェクトを作成して、デフォルトのページを期待どおりに表示しました。私は、次のテストコードで(同じプロジェクトで)新しいクラスを追加しました:ASP.Net Core Integration Testing

[TestClass] 
public class HomeControllerTests 
{ 
    private HttpClient client; 

    [TestInitialize] 
    public void Initialize() 
    { 
     // Arrange 
     var host = new WebHostBuilder() 
      .UseEnvironment("Development") 
      .UseKestrel() 
      .UseContentRoot(Directory.GetCurrentDirectory()) 
      .UseIISIntegration() 
      .UseStartup<Startup>(); 

     TestServer server = new TestServer(host); 

     client = server.CreateClient(); 
    } 


    [TestMethod] 
    public async Task CheckHomeIndex() 
    { 
     string request = "/"; 

     var response = await client.GetAsync(request); 

     response.EnsureSuccessStatusCode(); 

     Assert.IsTrue(true); 
    } 
} 

テストは、以下の例外を除いて、合格しない:

を表示「をインデックス」が見つかりませんでした。次の場所を検索した:
/Views/Home/Index.cshtml
を /Views/Shared/Index.cshtml

は、この段階でのxUnitとは対照的に、私はMSTestをを使用しています。 ContentRootを「複製」の質問に示唆されたものに変更することは、残念ながら問題を解決しません。

誰かが統合テストを行っていますか?私はWebHostBuilderの設定を微調整しようとしましたが、運がありません。

+0

[なぜTestServer(AspNetCore)が静的ファイルで404エラーを出すのか?](http://stackoverflow.com/questions/37815267/why-the-testserver-aspnetcore-gives-404-error-on- static-files) – Set

+0

残念ながら、そこにある解決策では問題は解決しません。私はContentRootをソリューションのそれ、wwwrootとその間のすべてのものに変更しようとしましたが、違いはありません。 – Calanus

答えて

6

私は、統合テスト(MVCをカバーしていない)に関するブログ記事を書きました。また、「Snow Crash」も同様の問題をコメントしました。彼らは次のコードで 'not found'エラーを解決しました:

var path = PlatformServices.Default.Application.ApplicationBasePath; 
var setDir = Path.GetFullPath(Path.Combine(path, <projectpathdirectory>)); 

var builder = new WebHostBuilder() 
    .UseContentRoot(setDir) 
    .UseStartup<TStartup>(); 

ブログの投稿はIntroduction to integration testing with xUnit and TestServer in ASP.NET Coreです。