There is no argument given that corresponds to the required formal parameter 'cancellation' of

Viewed 32

I have this interface which I am creating XUnit testing.

Task PostMessage(CancellationToken cancellation);

This is my XUnit code

    [Fact]
    public async Task WhenAValidMessagePosted_ThenReturnStatus20()
    {
        // Arrange (Seed)         

        // Act
        var result = await _service.PostMessage();

        // Assert
    }

I have compilation error here in PostMessage(). How do I supply CancellationToken to this method in Xunit?

1 Answers

If you just need a placeholder CancellationToken, pass in CancellationToken.None:

var result = await _service.PostMessage(CancellationToken.None);

However, in my experience, you don't want to risk deadlocking your entire test suite, so I generally prefer to pass a real cancellation token that will automatically cancel after a couple of seconds. You can accomplish this by using a CancellationTokenSource:

CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
source.CancelAfter(TimeSpan.FromSeconds(5));
var result = await _service.PostMessage(token);
Related