Unit-testing rendering views with Razor Engine not working in dotnet core 3.x
See original GitHub issueDescribe the bug
Unit-testing rendering of Razor Views using Microsoft.AspNetCore.Mvc.Testing worked in dotnet core 2.2, but in 3.1 (and 3.0) the Razor Engine does not locate my views any more specified like this:
builder.UseSolutionRelativeContentRoot("Rendering");
To Reproduce
I have made a small example that reproduces the issue in the dotnet core 3.1 solution (while in the dotnet core 2.2 solution the view is found).
https://github.com/toralux/RazorEngineRendering
- dotnetcore3.1
- dotnetcore2.2
The only difference between the solutions is the framework (TargetFramework is netcoreapp3.1 instead of netcoreapp2.2) and version of nuget packages (Microsoft.AspNetCore.Mvc and Microsoft.AspNetCore.Mvc.Testing) (version 3.1.0 instead of 2.2.0).
The part of the example that don’t find the view:
var viewEngineResult = _viewEngine.FindView(actionContext, name, false);
if (!viewEngineResult.Success) // <-- here 2.2 has found the view and 3.1 has not
{
throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", name));
}
Further technical details
- ASP.NET Core version: 3.1 (and 3.0)
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (2 by maintainers)
Top Results From Across the Web
How to compile razor views in unit test project
The first thing I would check is that your UnitTest project is targetting the identical net.core version as your web project.
Read more >Razor Pages unit tests in ASP.NET Core
ASP.NET Core supports unit tests of Razor Pages apps. Tests of the data access layer (DAL) and page models help ensure:.
Read more >Why is testing MVC Views frowned upon?
Unit testing the view rendered as expected is just unit testing that your templating engine works. That's like unit testing your compiled C ......
Read more >On .NET Live - The cutting edge of the Razor engine - YouTube
NET with free self-guided learning from Microsoft Learn: http://aka.ms/learndotnet ... NET Core Console application | Razor View Engine.
Read more >View Components in Razor Pages
View components are particularly useful for data-driven features in a layout page where there is no related page model or controller class.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I tried this and now it seems to be working:
Makes me wonder why it worked in 2.1.
Setting an explicit content root via
UseContentRoot
also has no effect.