Using Microsoft.aspnetcore.testhost I am unable to make http get to controller action returning razor view. I need to test the response has specific security headers and have other test I would like to perform on the action responses. Razor throws compilation exceptions for every namespace it hits, not just the system example below:
oclq12bb.ugz(5,11): error CS0246: The type or namespace name 'System' could
not be found (are you missing a using directive or an assembly reference?)
oclq12bb.ugz(6,11): error CS0246: The type or namespace name 'System' could
not be found (are you missing a using directive or an assembly reference?)
Repro:
- Using visual studio 2017 create a new asp.net core 1.1 web project and add a .net core unit test project.
- Remove the appsettings from startup.
- Set view /home/index.cshtml to copy to output directory to "copy always".
- create and run unit test. Throws exception.
Code:
[TestClass]
public class UnitTest1
{
private readonly HttpClient _client;
public UnitTest1()
{
var server = new TestServer(
new WebHostBuilder().UseStartup<Startup>());
_client = server.CreateClient();
}
[TestMethod]
public async Task Test1()
{
var response = await _client.GetAsync("/");
}
}
I have tried publishing the web project to a folder and using the .UseContentRoot() in place of copy always. Same result.