Testing ASP.Net Core Integration

I'm struggling to get any integration tests working with ASP.Net Core RC2. I created a basic web project that works fine in the browser, showing the default page as expected. Then I added a new class (in the same project) with the following test code:

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

The test fails with the following exception:

Index view not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml

I am using MSTest, not xUnit at this point. Unfortunately, changing ContentRoot to what was suggested in the "recurring" question does not solve the problem.

Has anyone received integration tests? I tried to configure the settings of WebHostBuilder , but no luck.

+4
source share
1 answer

I wrote a blog post on integration testing (without covering MVC), and "Snow Crash" commented on a similar issue. They solved the "not found" error with the following code:

 var path = PlatformServices.Default.Application.ApplicationBasePath; var setDir = Path.GetFullPath(Path.Combine(path, <projectpathdirectory> )); var builder = new WebHostBuilder() .UseContentRoot(setDir) .UseStartup<TStartup>(); 

A blog post is here Introduction to integration testing with xUnit and TestServer in ASP.NET Core .

+6
source

Source: https://habr.com/ru/post/1271768/


All Articles