Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check calls Received() for async method

Tags:

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?

like image 962
letitbe Avatar asked Jun 23 '15 07:06

letitbe


People also ask

How do you check if command execute (); has been received by a substitute?

In some cases (particularly for void methods) it is useful to check that a specific call has been received by a substitute. This can be checked using the Received() extension method, followed by the call being checked. In this case command did receive a call to Execute() , and so will complete successfully.

What happens when you call async method?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

How do you call async method?

The simplest way to execute a method asynchronously is to start executing the method by calling the delegate's BeginInvoke method, do some work on the main thread, and then call the delegate's EndInvoke method. EndInvoke might block the calling thread because it does not return until the asynchronous call completes.

What is async return method?

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. Note: Even though the return value of an async function behaves as if it's wrapped in a Promise.resolve , they are not equivalent.


1 Answers

I found an answer here.

Received.InOrder(async () => {     await _Commands.UpdateAsync(Arg.Is<Lobby>(l => l.Status == Status.Updated)); }); 
like image 186
letitbe Avatar answered Oct 24 '22 21:10

letitbe