question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit-testing rendering views with Razor Engine not working in dotnet core 3.x

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
brockallencommented, Feb 3, 2020

I tried this and now it seems to be working:

            var builder = new WebHostBuilder();
            builder.ConfigureAppConfiguration((context, b) => {
                context.HostingEnvironment.ApplicationName = typeof(HomeController).Assembly.GetName().Name;
            });

Makes me wonder why it worked in 2.1.

1reaction
brockallencommented, Jan 31, 2020

Setting an explicit content root via UseContentRoot also has no effect.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found