Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

How to mock Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet? #890

Closed
Daniel-Khodabakhsh opened this issue May 30, 2019 · 7 comments

Comments

@Daniel-Khodabakhsh
Copy link
Contributor

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

  1. Create a class library of type Microsoft.NET.Sdk.Razor and add views which use Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet.
  2. Create an xUnit test project of type Microsoft.NET.Sdk.Web and add the class library as a project reference.
  3. 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

@Daniel-Khodabakhsh Daniel-Khodabakhsh changed the title How to mock Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet How to mock Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet? May 30, 2019
@cijothomas
Copy link
Contributor

cijothomas commented May 30, 2019

can you add like this in your startup.cs ?
services.AddSingleton<JavaScriptSnippet>();

@Daniel-Khodabakhsh
Copy link
Contributor Author

can you add like this in your startup.cs ?
services.AddSingleton<JavaScriptSnippet>();

Thanks for the suggestion.
When doing this I get the following error:

[xUnit.net 00:00:01.43]       System.InvalidOperationException : Unable to resolve service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' while attempting to activate 'Microsoft.ApplicationInsights.AspNetCore.JavaScriptSnippet'.

Which sounds like I need to add a mock Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.

I guess I could create an interface that both the unit test and production runtimes will use, and then from that I can call JavaScriptSnippet.FullScript.

@cijothomas
Copy link
Contributor

@Daniel-Khodabakhsh do you need any additional help? Closing this for now. reopen if you have questions.

@Daniel-Khodabakhsh
Copy link
Contributor Author

@cijothomas May I change this question into a feature request?
The request being the creation of an IJavaScriptSnippet interface that I can use to mock, and to have ApplicationInsights register via this interface as well.

@cijothomas
Copy link
Contributor

Will tag as enhancement.
Will try to address along with the following, but not guaranteeing timelines :)
#866

We gladly accept a pull request, if you have time for this as well.

@Daniel-Khodabakhsh
Copy link
Contributor Author

Thanks @cijothomas!
I created a pull request, how can I get someone to review it?

@cijothomas
Copy link
Contributor

Just saw it. We'll review it next week.

@cijothomas cijothomas added this to the 2.8.0 milestone Jun 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants