Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda