Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda