It is my first time when I write unit tests for async method. I am using xUnit. I searched SO with no promissing results. Best I found, but didnt work for me, is to implement IAsyncLifetime
from THIS example. I will be thankful for any hints how to trouble shoot this problem.
Currently what I have. In tested VM I have a command:
public ICommand TestResultsCommand { get; private set; }
And the command is initialized in the VM constructor as below:
TestResultsCommand = new DelegateCommand(OnTestResultExecuteAsync);
Command calls method:
private async void OnTestResultExecuteAsync(object obj)
{
TokenSource = new CancellationTokenSource();
CancellationToken = TokenSource.Token;
await TestHistoricalResultsAsync();
}
TestHistoricalResultsAsync method's signature is as below:
private async Task TestHistoricalResultsAsync()
Now lets go to the unit test project. Currently in test class I have a method:
[Fact]//testing async void
public void OnTestResultExecuteAsync_ShouldCreateCancellationTokenSource_True()
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken cancellationToken = tokenSource.Token;
_viewModel.TestResultsCommand.Execute(null);
Assert.Equal(cancellationToken.CanBeCanceled, _viewModel.CancellationToken.CanBeCanceled);
Assert.Equal(cancellationToken.IsCancellationRequested, _viewModel.CancellationToken.IsCancellationRequested);
}
And the test drops me an exception:
Message: System.NullReferenceException : Object reference not set to an instance of an object.
The stack trace for the exception is:
Thank you in advance for your time and suggestions.