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?
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());
}