-
Notifications
You must be signed in to change notification settings - Fork 2.1k
TestServer against MVC application can't find Razor Views. #3410
Comments
Have you had a look at our functional tests and specifically |
@pranavkm yes, it's pretty good. I used it to understand how it works. Wouldn't be good to have something like that out of the box? |
@Bartmax to a certain extent I agree, I think there's some things MVC requires that we won't put in the 'testserver' box. Setting up the base path is something we might want to consider. |
Sure, just let me know if you want me to help or contribute in any way. |
This is likely a dup of #6233 |
We're shipping a package in 2.0.0 that will address all of these issues. See: https://github.com/aspnet/Mvc/tree/dev/src/Microsoft.AspNetCore.Mvc.Testing |
@rynowak Is it correct that the package is not available on nuget?! https://www.nuget.org/packages?q=microsoft.aspnetcore.mvc.testing:
Because it exists on the myget feed. It is also mentioned in the release notes but I cannot find the assembly on the official channels. |
I'm trying to test an action thats return a Razor View and I'm experiencing some troubles because always is returning a 404. If I check the stacktrace the exception is "An unhandled exception has occurred: The view 'About' was not found". I'm checking here that is going to be released in 2.1.0 version. The view exists in the folder. This my test code. public class StringLocalizerTest
{
private readonly TestServer _server;
private readonly HttpClient _client;
public StringLocalizerTest()
{
var webHostBuilder = new WebHostBuilder()
.UseStartup<Startup>();
_server = new TestServer(webHostBuilder);
_client = _server.CreateClient();
}
[Fact]
public async Task Any_Test()
{
var response = await _client.GetAsync("/about");
var content = await response.Content.ReadAsStringAsync();
}
} This is my controller code. public class HomeController : Controller
{
// GET home/index
[HttpGet]
public IActionResult Index()
{
return View();
}
// GET home/about
[HttpGet("about")]
public IActionResult About()
{
return View();
}
} This is the stacktrace.
|
As of right now, when you use
TestServer
on a Test project against an MVC application with Razor Views those are not found becauseApplicationBasePath
is the Test project one and not the MVC.The solution is to change the
ApplicationBasePath
of the Test project to the MVC Application.Changing the base path is not a straightforward thing to do, in terms that for every test project that needs this, one must implement
IApplicationEnvironment
and hook into theTestServer
somehow.I created a package at https://github.com/Bartmax/TestServerMvcHelper which hooks into
WebHostingBuilder
and does exactly that with one practical convention of where to look for the Application.I wanted to start a discussion to see if there's interest on the team for this (or similar) feature, as I think this should be provided/baked into the framework, with a maybe smarter way for application basepath discovery.
The text was updated successfully, but these errors were encountered: