New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ArgumentNullException when performing AspNetCore Integration Tests using Microsoft.AspNetCore.TestHost.TestServer
#202
Comments
I'm getting the same exception in a .NET Framework 4.6.1 MSTest project -- but not when I'm running the ASP.NET Core project... operatingAssembly must be null from a test project? |
+1 |
Too many problems with this project. I finally must give up. |
Just pitfall after pitfall. Absolutely brutal. |
Having the same issue, trying to unit test my code. |
you can get a step closer by on your engine builder adding .SetOperatingAssembly(Assembly assembly) however this will still error but with a different error message regarding DependencyContext. |
I have a .netframework project and I had a similar issue. You need to call "SetOperatingAssembly" and pass the assembly where you actually use the razor engine. To make this work I created a factory that is easily injectable and it can create the razor engine wherever and whenever I need it.
Just inject this factory and use it to create the razor engine instead of directly injecting the engine. |
@BarsikV Thanks. Please update the README.md via PR. Very easy to do and doesn't require cloning the git repo locally. That said, as a code/peer review, perhaps Create() should take a parameter to avoid JIT in-line optimization changing the calling assembly value: public class RazorEngineFactory : IRazorEngineFactory
{
public IRazorLightEngine Create(Assembly operatingAssembly = null)
{
return new RazorLightEngineBuilder()
.SetOperatingAssembly(operatingAssembly ?? Assembly.GetCallingAssembly())
.UseFileSystemProject(Directory.GetCurrentDirectory())
.UseMemoryCachingProvider()
.Build();
}
} you can then call |
I ended up implementing the interface from #202 (comment) as RazorLightEngineWithFileSystemProjectFactory. It is a somewhat superfluous interface, but most people are probably calling RazorLight with a file system, so it makes sense to give them something standard to hook into. It also frankly will make reading tests easier. |
Describe the bug
ArgumentNullException when performing AspNetCore Integration Tests using
Microsoft.AspNetCore.TestHost.TestServer
To reproduce:
Run a test class library targeting .NET Framework 4.7.1. but running a .NETCore api. Inject the RazorlightEngine in a controller, query the controller end point and wait for it to throw.
Information (please complete the following information):
The text was updated successfully, but these errors were encountered: