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

281
Vistas
Check calls Received() for async method

When I run the following code:

[Test]
public async Task Can_Test_Update()
{
    var response = await _controller.UpdateAsync(Guid.NewGuid());
    response.Valid.Should().BeTrue();

    _commands.Received().UpdateAsync(
        Arg.Is<Something>(
            l => l.Status == Status.Updated)); 
}

If I add "await" preceding the "_commands.Received().UpdateAsync", it throws a null reference exception. How can I stop this happening, or is await not necessary?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I found an answer here.

Received.InOrder(async () =>
{
    await _Commands.UpdateAsync(Arg.Is<Lobby>(l => l.Status == Status.Updated));
});
over 4 years ago · Santiago Trujillo Denunciar

0

When NSubstitute sees an async call it automatically creates a completed task so the await works as you would expect in your code (and not throw a NullReferenceException). In this case that would be the task returned from _commands.UpdateAsync(Status.Updated)) inside the method you are testing.

The .Received() call on the other hand is verifying that the async method was called, that is fully synchronous so it doesn't need to be awaited.

The key thing to remember is that async methods return a Task. Calling the async method and returning the task is fully synchronous, you then await the Task to know when the asyncronous operation which the task represents is completed.

over 4 years ago · Santiago Trujillo Denunciar

0

According to this answer on Stack Overflow, as of NSubstitute version 1.8.3 you can use await and it will work as expected, rather than throwing a NullReferenceException.

I've just tried it out as I was on version 1.5.0 and getting the NullReferenceException as you describe, but now I'm on the latest (1.10.0), it's working well.

over 4 years ago · Santiago Trujillo 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