How to mock Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet? #890
Description
Background
I have a project which is testing views served from a class library (Microsoft.NET.Sdk.Razor
).
In order to do this, the test project itself is of MsBuild SDK type Microsoft.NET.Sdk.Web
.
The views in the class library use the injected Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet
.
When I run a unit test where an HttpClient
tries to render the view, I get the following error:
[xUnit.net 00:00:01.58] xxx.Frontend.Test.PageTests.Index_matches_snapshot [FAIL]
[xUnit.net 00:00:01.58] System.InvalidOperationException : No service for type 'Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet' has been registered.
[xUnit.net 00:00:01.58] Stack Trace:
[xUnit.net 00:00:01.58] at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorPagePropertyActivator.<>c__DisplayClass8_0.<CreateActivateInfo>b__1(ViewContext context)
[xUnit.net 00:00:01.58] at Microsoft.Extensions.Internal.PropertyActivator`1.Activate(Object instance, TContext context)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Razor.Internal.RazorPagePropertyActivator.Activate(Object page, ViewContext context)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Razor.RazorPageActivator.Activate(IRazorPage page, ViewContext context)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderLayoutAsync(ViewContext context, ViewBufferTextWriter bodyWriter)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResultExecutedContext context)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Routing.EndpointMiddleware.Invoke(HttpContext httpContext)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.TestHost.HttpContextBuilder.<>c__DisplayClass10_0.<<SendAsync>b__0>d.MoveNext()
[xUnit.net 00:00:01.58] --- End of stack trace from previous location where exception was thrown ---
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.TestHost.ClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Testing.Handlers.CookieContainerHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
[xUnit.net 00:00:01.58] at Microsoft.AspNetCore.Mvc.Testing.Handlers.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
[xUnit.net 00:00:01.58] at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
[xUnit.net 00:00:01.58] D:\a\1\s\src\xxx.Testing.Snapshots\AssertSnapshot.cs(77,0): at xxx.Testing.Snapshots.AssertSnapshot.ToMatchHtmlResponse(HttpClient httpClient, Uri url, String methodName, String filePath)
[xUnit.net 00:00:01.58] D:\git\xxx\xxx\test\xxx.Frontend.Test\PageTests.cs(15,0): at xxx.Frontend.Test.PageTests.Index_matches_snapshot()
[xUnit.net 00:00:01.58] --- End of stack trace from previous location where exception was thrown ---
Question
How can I mock Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet
?
I tried adding singleton via my test's StaptUp.cs - ConfigureServices(IServiceCollection ...)
:
services.AddSingleton(JavaScriptSnippet, JavaScriptSnippet);
but this fails because JavaScriptSnippet
is a type.
The JavaScriptSnippet
metadata doesn't show any interface it's using so I'm not able to register my own mock.
Repro Steps
- Create a class library of type
Microsoft.NET.Sdk.Razor
and add views which useMicrosoft.ApplicationInsights.AspNetCore.JavaScriptSnippet
. - Create an xUnit test project of type
Microsoft.NET.Sdk.Web
and add the class library as a project reference. - Run the test.
Actual Behavior
The above callstack occurs.
Expected Behavior
The test should complete without the exception.
Version Info
SDK Version : 2.2.204
.NET Version : dotnet-core 2.2
How Application was onboarded with SDK: VisualStudio
OS : Windows 10
Hosting Info : xUnit