I have a conditional statement which should looks as follows:
//...
if(_view.VerifyData != true)
{
//...
}
else
{
_view.PermanentCancellation.Cancel();
}
where PermanentCancellation is of type CancellationTokenSource.
Im wondering how i should set this up in my mock of _view. All attempts thus far have failed 🙁 and i cant find an example on google.
Any pointers would be appreciated.
Because CancellationTokenSource.Cancel is not virtual you cannot mock it with moq.
You have two options:
Create a wrapper interface:
and an implementation which delegates to the wrapped CancellationTokenSource
And use the
ICancellationTokenSourceasPermanentCancellationthen you can create anMock<ICancellationTokenSource>in your tests:And use the
CancellationTokenSourceWrapperin your production code.Or use a mocking framework which supports mocking non virtual members like: