moq icon indicating copy to clipboard operation
moq copied to clipboard

FileNotFoundException: Microsoft.AspNetCore.Razor.Runtime

Open tiagodenoronha opened this issue 8 years ago • 8 comments

Good afternoon,

I'm having some trouble figuring out why i'm getting this specified error when mocking an aparently normal object. The exception is Message: System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

I can provide a concrete test case where this happens. https://github.com/tiagodenoronha/PlantsIdentifier/blob/feature/authentication/tests/PlantsIdentifierAPI.UnitTests/Controllers/LoginControllerTests.cs#L27

Running this test with the commented line fails.

Is it something i'm doing wrongly on my side?

Thanks!

tiagodenoronha avatar Oct 22 '18 14:10 tiagodenoronha

This will likely be due to an assembly version conflict, or an invalid assembly binding redirect, and nothing directly related to Moq. Check if there are any warnings during the build or in the build log.Check for NuGet version conflicts & update packages where necessary. Check whether your assembly binding redirects are up-to-date & correct (if you have any at all). Possibly delete them all and configure your project to generate them automatically during build. Make sure you're using up-to-date tool versions. That's the usual suggestions I'd hand out to anyone having this kind of problem.

Closing this issue, since it is very likely unrelated to Moq. If you think this is in error, and there's something that needs to be done at Moq's end, please let us know.

stakx avatar Oct 22 '18 16:10 stakx

I don't have that assembly installed on my test project. Could that be it? But why would that assembly be needed for such a simple class with 5 properties? No warnings, no nuget updates, and no assembly binding redirects :/

It's probably an error on my side, because it doesn't make sense that it has anything to do with Moq. Was just wondering if anyone had already experienced a similar issue.

tiagodenoronha avatar Oct 22 '18 21:10 tiagodenoronha

No warnings, no nuget updates, and no assembly binding redirects :/

TokenModel appears to be defined in your main project, which references Microsoft.AspNetCore.App, which has a dependency on Microsoft.AspNetCore.Razor, which in turn depends on Microsoft.AspNetCore.Razor.Runtime. So it seems to me that the latter assembly should be around.

Perhaps try publishing your application and see what dependencies get copied to the publish/ output directory. Or check your NuGet package cache to see if the package (in the right version) was at least downloaded from NuGet.

Sometimes the "Consolidate NuGet packages" dialog in VS (accessible via the context menu on the solution node in Solution Explorer) gives hints about possible NuGet version conflicts.

Perhaps also increase the build verbosity level to see if you have missed any messages.

But why would that assembly be needed for such a simple class with 5 properties?

I cannot tell, to be honest. The problem doesn't seem to lie with TokenModel itself, since your method already successfully references that type now (new TokenModel()), and that type has only simple properties with primitive types which makes it seem unlikely that JIT-compiling this type would cause the JIT to look for any ASP.NET Core assembly.

(That being said, Mock.Of<T>() does perform a whole lot more work than new T(), in particular it executes a mock.SetupAllProperties(), meaning it'll visit all properties in your type and cause these properties' types to be JIT-compiled if needed) — if you still suspect there's something inside Moq going wrong, you could try to disable VS' "Just My Code" setting and step into the Mock.Of<TokenModel>() call (Moq comes with SourceLink enabled) to see how far execution gets.)

If this turns out to be a problem with Moq after all, I am more than happy to reopen this issue.

stakx avatar Oct 23 '18 07:10 stakx

Even with the publish the dll doesn't appear in the folder, which makes it even stranger... It's most likely a problem on my side, just can figure out what :) Thanks anyways!

tiagodenoronha avatar Oct 26 '18 15:10 tiagodenoronha

@tiagodenoronha - I cloned your repository and ran the unit tests. They all pass. So yes, I'd say this is a problem with your setup or tooling.

stakx avatar Oct 26 '18 15:10 stakx

Well I installed Microsoft.AspNetCore.Razor.Runtime and aparently it now works... Weird..

tiagodenoronha avatar Oct 27 '18 23:10 tiagodenoronha

I am still getting an error message

"System.TypeLoadException: 'Could not load type 'Microsoft.AspNetCore.Razor.Parser.ParserBase' from assembly 'Microsoft.AspNetCore.Razor, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.'"

I included all of the following packages:

Microsoft.AspNetCore.Razor Microsoft.AspNetCore.Razor.Runtime RazorEngine System.Configuration.ConfigurationManager

andrewtheart avatar Apr 12 '19 18:04 andrewtheart

I had a controller that was returning an anonymous class and in a xunit test I wanted to check its value. Following this answer, I set the return value to dynamic (wrapped in an OkObjectResult in an IActionResult) and encountered the above error in my unit test project:

FileNotFoundException: Microsoft.AspNetCore.Razor.Runtime

Manually installing Microsoft.AspNetCore.Razor.Runtime v. 2.2.0 fixed the problem. But dotnet should have informed me about a missing package at compile time and not at runtime, right?

Sword-Smith avatar Aug 12 '19 10:08 Sword-Smith