Using bUnit to mock IJSRuntime when passing a DotNetObject reference to the JavaScript method. #1366
-
I have the following code in my component that calls uses IJSRuntime to call a JavaScript function and passes in a DotNetObject reference as an argument:
In my test, I have tried setting this up as: When trying to run the test, the following exception is thrown when calling
Am I missing something, or is this not currently supported? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey @MorneZaayman the short answer is that you can't use the Moq call to setup the Mock ( There are two ways:
var jsruntime = Mock.Of<IJSRuntime>();
// This replaces the Bunit version of the JSInterop with the Mock
Services.AddScoped(_ => jsruntime.Object);
jsruntime.Setup(s => s.InvokeAsync<IJSVoidResult>("select2", "#TestLabel", "Please select an option...", It.IsAny<DotNetObjectReference<BuiInputSelect<string>>>());
JSInterop.SetupVoid(m => m is { Identifier: "select2", Arguments.Count: 3 }
&& m.Arguments[0] as string == "#TestLabel"
&& m.Arguments[1] as string == "Please select an option...")
.SetVoidResult(); |
Beta Was this translation helpful? Give feedback.
-
Thank you for the detailed reply. I modified your second example to ensure that IJSRuntime is called with the correct type for the third argument, but otherwise it works perfectly.
|
Beta Was this translation helpful? Give feedback.
Hey @MorneZaayman
the short answer is that you can't use the Moq call to setup the Mock (
It.IsAny<DotNetObjectReference<BuiInputSelect<string>>>()
) as bUnit doesn't have an understanding of what.Is.IsAny
means.There are two ways:
IJSRuntime
in your test:InvocationMatcher
(the_ => true
part) and fine tune: