Skip to content

Using bUnit to mock IJSRuntime when passing a DotNetObject reference to the JavaScript method. #1366

Closed Answered by linkdotnet
MorneZaayman asked this question in Q&A
Discussion options

You must be logged in to vote

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:

  1. Either you completely register a mock of IJSRuntime in your test:
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>>>());
  1. Use the InvocationMatcher (the _ => true part) and fine tune:
JSInterop.SetupVoid(m 

Replies: 2 comments

Comment options

linkdotnet
Feb 3, 2024
Maintainer Sponsor

You must be logged in to vote
0 replies
Answer selected by MorneZaayman
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants