'How to correctly run an async thread in a test method?

I have this test that needs to run the RunWebSocket thread to make sure the SendAsync was received by my websocket. This works, but I am getting compiler warning CS4014. Of course, I do not like warnings, so instead of ignoring it, I want to do it "right".

[Fact]
public async void CabinetTagPushed_ShouldSendBroadcastToWebSocket()
{
    // arrange
    var websocket = Substitute.For<IWebSocket>();
    restinterface.RunWebSocket(websocket);

    // act
    restinterface.Cabinet_OnTagPushed(null, new TagEventArgs());

    // assert
    await websocket.Received().SendAsync(Arg.Any<ArraySegment<byte>>(), WebSocketMessageType.Text, false, CancellationToken.None);

    // this will end the RunWebSocket thread
    websocket.CloseStatus = WebSocketCloseStatus.NormalClosure;
}

How can I create a test that kicks off an async thread correctly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source