Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
Mock IJSRuntime InvokeVoidAsync

I want to mock InvokeVoidAsync from C# Blazor IJSRuntime with using xunit and Moq. So far I have a simple function to test that calls await _js.InvokeVoidAsync("register"), where _js is an object of type IJSRuntime.

My test looks like the following:

[Fact]
public async Task Register_Test()
{
    var jsMock = new Mock<IJSRuntime>();
    jsMock.Setup(x => x.InvokeAsync<object>(It.IsAny<string>(), It.IsAny<object[]>())).Returns(new ValueTask<object>());

    ...

    jsMock.Verify(x => x.InvokeAsync<bool>("register"), Times.Once());
}

Note that I do not call InvokeVoidAsync directly as stated here and I'm trying to apply the solution from here that describes how to used InvokeAsync<type>.

This test compiles, but while executing I get an error saying

System.InvalidCastException : Unable to cast object of type 'System.Threading.Tasks.ValueTask`1[System.Object]' to type 'System.Threading.Tasks.ValueTask`1[Microsoft.JSInterop.Infrastructure.IJSVoidResult]'.

How can I call and test InvokeVoidAsync from IJSRuntime?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The error message was quite helpful.

The correct test would look like this:

{
    var jsMock = new Mock<IJSRuntime>();
    jsMock.Setup(x => x.InvokeAsync<Microsoft.JSInterop.Infrastructure.IJSVoidResult>(It.IsAny<string>(), It.IsAny<object[]>())).Returns(new ValueTask<Microsoft.JSInterop.Infrastructure.IJSVoidResult>());

    ...

    jsMock.Verify(x => x.InvokeAsync<Microsoft.JSInterop.Infrastructure.IJSVoidResult>("register"), Times.Once());
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!