Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Intermittent failure to find cshtml files by NCrunch
akilin
#1 Posted : Saturday, December 6, 2025 12:05:42 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/5/2025(UTC)
Posts: 6
Location: Ukraine

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Hi, I have, over the past few months, encountered an issue with tests working from from CLI using `dotnet test` command, but failing from NCrunch VS22/VS26 runner window.

The setup looks something like this:

We have an `EmailGenerator.csproj` project that is of type <Project Sdk="Microsoft.NET.Sdk.Razor">
It's used as a library project that is referenced by other API/Console type projects.
Inside of it are multiple .cshtml files that we use to generate html emails.
The code inside is based on:
https://scottsauber.com/...standard-class-library/

We have an e2e xunitv3 test project that runs some tests.
The tests always work fine when run via `dotnet test`.

When running via NCrunch - once in a few days/weeks, they start failing with the error:
System.InvalidOperationException: Unable to find view '/Views/Emails/EmailConfirmation/EmailConfirmation.cshtml'. The following locations were searched:
/Views/Emails/EmailConfirmation/EmailConfirmation.cshtml

Basically, some files cannot be found.
Whenever this happens, running `dotnet test` once fixes the NCrunch issue, and after that, NCrunch is able to find the view and run the tests.

It's not a dealbreaker, as there is a workaround (dotnet test).
And I can also always substitute the actual class with some mock implementation as an alternative.
But we are trying to test as close to the real production as we can. So ideally, I would love to make it work the same way it does when run via ' dotnet test`.

I would appreciate any suggestions/pointers. I am hoping that maybe I have misconfigured something, and this is solvable by setting some flag in settings. But so far, I was unable to find anything that looked related.




Remco
#2 Posted : Saturday, December 6, 2025 2:18:54 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,444

Thanks: 1011 times
Was thanked: 1357 time(s) in 1260 post(s)
Hi, thanks for sharing this issue.

Can you share any details about how you're finding the .cshtml files in your code? This feels like you have an implicit dependency on them that NCrunch isn't aware of, so it's cycling workspaces underneath your tests while they're running.
akilin
#3 Posted : Saturday, December 6, 2025 2:12:16 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/5/2025(UTC)
Posts: 6
Location: Ukraine

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
We use the code from this article:
https://scottsauber.com/...standard-class-library/

Specifically this section of it:

Code:
    
private IView FindView(ActionContext actionContext, string viewName)
    {
        var getViewResult = _viewEngine.GetView(executingFilePath: null, viewPath: viewName, isMainPage: true);
        if (getViewResult.Success)
        {
            return getViewResult.View;
        }

        var findViewResult = _viewEngine.FindView(actionContext, viewName, isMainPage: true);
        if (findViewResult.Success)
        {
            return findViewResult.View;
        }

        var searchedLocations = getViewResult.SearchedLocations.Concat(findViewResult.SearchedLocations);
        var errorMessage = string.Join(
            Environment.NewLine,
            new[] { $"Unable to find view '{viewName}'. The following locations were searched:" }.Concat(searchedLocations));

        throw new InvalidOperationException(errorMessage);
    }
akilin
#4 Posted : Saturday, December 6, 2025 2:29:02 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/5/2025(UTC)
Posts: 6
Location: Ukraine

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
I am not very familiar with how Razor handles cshtml files. But I couldn't find any individual files with names matching the view names inside the `bin` folder, so it probably compiles them into the single resulting dll, while keeping the naming/path information.
And then when we call `viewEngine.FindView` it retrieves the view via that information. But I am speculating here. I am not sure how Razor and cshtml files work under the hood.

---

I am also not very familiar with how NCrunch works under the hood, but if this were a case of missing an implicit dependency, wouldn't it always fail when being run via NCrunch?
As it stands, it fails sometimes. But whenever it does fail, I can run `dotnet test` once, and after that NCrunch issue is fixed. It can successfully run the tests and find the view files.
Remco
#5 Posted : Sunday, December 7, 2025 12:52:28 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,444

Thanks: 1011 times
Was thanked: 1357 time(s) in 1260 post(s)
Looking under the hood at how this works, I think the key here is how the root path to the Razor views is being set. This will likely depend on your test code (i.e. WebApplication.CreateBuilder / Host.CreateDefaultBuilder). It may be dependent on Directory.GetCurrentDirectory(), so if you have any code that changes the current directory, this might be a cause of intermittent behaviour depending on the sequence of your tests.

When NCrunch runs tests, it copies your code out into workspaces. The workspaces are then wired together to build runtime environments for the tests. This wiring considers the references between assemblies, but it doesn't factor in non-compiled resource dependencies that might exist between projects (for example, your test project depends on your Razor views that are in your web project, which are not copied or embedded resources).

There are facilities in NCrunch to handle situations like this, but it involves using the NCrunch Environment class (i.e GetAllAssemblyLocations()) in your test code to find the workspace containing the web code (and therefore the Razor views).

It's not clear for me why this is mostly working for you at the moment. Perhaps you have an absolute path reference that points at the foreground projects you have open in visual studio, which can cause these sorts of problems under NCrunch. This might explain why running 'dotnet test' fixes the problem after it appears.
akilin
#6 Posted : Sunday, December 7, 2025 2:07:50 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/5/2025(UTC)
Posts: 6
Location: Ukraine

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Quote:
This will likely depend on your test code (i.e. WebApplication.CreateBuilder / Host.CreateDefaultBuilder). It may be dependent on Directory.GetCurrentDirectory()

WebApplication.CreateBuilder is used when wiring things up at start of the app.
We do not have any code that changes the current directory.
It does not depend on the sequence of the tests, as it happens even if I run tests one at a time from NCrunch window.


Quote:
it doesn't factor in non-compiled resource dependencies that might exist between projects (for example, your test project depends on your Razor views that are in your web project, which are not copied or embedded resources).

I think `.cshtml` files are a compiled resource. I think they are embedded into the dll during compilation (not 100% sure tho). Basically treated like `.cs` files.

We do not reference the `.cshtml` files outside of their project.
All the work with them is encapsulated inside of the project containing the views.
We have a class that exposes methods like:
`public string GenerateRegistrationConfirmationEMail(string confirmationCode);`
So all the work with vews happens inside of it, never leaving the project/dll.
Test project will have to reference the MailGenerator.csproj and call methods that are inside of it. Never working with views directly.

---
I will try to create a repo with a minimal reproduction and link it in this thread.
It might be tricky, as I don't know what actually causes it. But I'll do my best to come up with something.
Unfortunately, due to work and life stuff - I am not sure about the timeline.
akilin
#7 Posted : Sunday, December 7, 2025 3:28:50 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/5/2025(UTC)
Posts: 6
Location: Ukraine

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
I think I got a working reproduction.

I got a repo with a single test.
It always succeeds when running via `dotnet test`.
It always fails when run via NCrunch from Visual Studio.

link to folder inside repo:
https://github.com/akili...ter/ncrunch-razor-views

steps to reproduce:
Code:
git clone https://github.com/akilin/demos.git
cd .\demos\ncrunch-razor-views\tests\
dotnet test

dotnet test will succeed.

then open `ncrunch-razor-views.sln` in latest visual studio 2026.
enable ncrunch and run the tests. they will always fail.

---

The behaviour I was describing before, where running `dotnet test` once was fixing the NCrunch issue, was due to our internal code.

Short version of why `dotnet test` was fixing NCrunch is:

We have a `GlobalTestSetupFixture` that runs once before all the tests are executed and ensures the database exists and is seeded with data.
When seeding the DB, we create a user. Creating a user triggers email sending.
So global fixture, when run for the first time, always tries to generate the email.
Subsequent runs - we already have a seeded DB, so no emails.

NCrunch runs, db empty, tries to create user, tries to send email, fails.
`dotnet test` runs, db empty, seeds it. All is fine.
Subsequent NCrunch runs already see a seeded database and do not try to seed again. No email sending. All is good
Remco
#8 Posted : Sunday, December 7, 2025 10:12:11 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,444

Thanks: 1011 times
Was thanked: 1357 time(s) in 1260 post(s)
Thanks for the repro.

I've gone down a bit of a rabbit hole trying to get this to work for you, but I think I'm going to have to admit that you aren't likely to get this particular test structure working with NCrunch. This MS toolset essentially relies on magic to find the razor files, and my attempts to override its search paths to redirect it to the correct NCrunch workspace have resulted in failure. Probably it IS possible to get this to work, but it's going to require quite a lot of configuration, concessions, and u-turns before you get there. Unfortunately it just wasn't designed to be used with a continuous test runner that splits up projects from the parent solution.
1 user thanked Remco for this useful post.
akilin on 12/7/2025(UTC)
akilin
#9 Posted : Sunday, December 7, 2025 12:47:02 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/5/2025(UTC)
Posts: 6
Location: Ukraine

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Unfortunate, but thank you for trying.
In any case, creating this thread was beneficial for me.
While trying to create a reproduction for you, I got a much better understanding of what was happening and why. So I can now work around this much more easily.

I appreciate you trying to help. Thank you.
1 user thanked akilin for this useful post.
Remco on 12/7/2025(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.057 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download