Moq version: 3.1.416.3
We found a bug caused by an event not being unsubscribed. I am trying to write a unit test to verify that the event is unsubscribed from. Is it possible to verify this using Mock<T>.Verify(expression)
?
My initial thought was:
mockSource.Verify(s => s.DataChanged -= It.IsAny<DataChangedHandler>());
But apparently
An expression tree may not contain an assignment operator
Then I tried
mockSource.VerifySet(s => s.DataChanged -= It.IsAny<DataChangedHandler>());
But that gives me
System.ArgumentException: Expression is not a property setter invocation.
How can I verify that the unsubscribe has taken place?
How the event is used
public class Foo
{
private ISource _source;
public Foo(ISource source)
{
_source = source;
}
public void DoCalculation()
{
_source.DataChanged += ProcessData;
var done = false;
while(!done)
{
if(/*something is wrong*/)
{
Abort();
return;
}
//all the things that happen
if(/*condition is met*/)
{
done = true;
}
}
_source.DataChanged -= ProcessData;
}
public void Abort()
{
_source.DataChanged -= ProcessData; //this line was added to fix the bug
//other cleanup
}
private void ProcessData(ISource)
{
//process the data
}
}
Ignore the convoluted nature of the code, we're dealing with signals from external hardware. This actually makes sense for the algorithm.