0
votes

In an attempt to unit test my Blazor server-side components/pages I am using the TestHost provided on the Github repo SteveSandersonMS/BlazorUnitTestingPrototype, found reading this article. My pages make use of the blazor radzen components library

I have managed to inject the DbContext and all other components required by my own code, but the Radzen library requires an instance of the IJSRuntime interface and the TestHost must be provided an already instantiated object.

I checked the type instantiated by injecting an IJSRuntime in a view and it's a Microsoft.JSInterop.JSRuntime, which is an abstract class with a protected constructor. I've search throught the provided DLLs for an instantiable type that implemented IJSRuntime but couldn't find one.

The question: How to instantiate an implementation of IJSRuntime for manual injection ?

1

1 Answers

1
votes

Use a mock :

  • Add a package reference to Moq (you can use the mock lib of your choice, I prefer Moq)
  • Add a using statement to Moq namespace
using Moq;
  • Create the mock for IJSRuntime
var jsRuntimeMock = new Mock<IJSRuntime>();
  • Inject the mock in the TestHost DI
var host = new TestHost();
host.ConfigureServices(services =>
{
    services.AddSingleton(p => jsRuntimeMock.Object);
}