After coding the extension method (based on a GUI update when you run the event handler class in a separate thread? ):
public static class ControlExtensions
{
public static TResult InvokeEx<TControl, TResult> (this TControl control,
Func<TControl, TResult> func)
where TControl : Control
{
if (control.InvokeRequired)
return (TResult)control.Invoke (func, control);
return func (control);
}
}
I am trying to use unit test this method both from the user interface thread and from the normal thread, and I cannot achieve this.
Here is the unit test code:
[Test]
public void TestInvokeExWithMethodReturningResultOnOtherThread ()
{
string result = string.Empty;
var form = new Form ();
var thread = new Thread (() =>
{
result = form.InvokeEx (f => f.Text);
});
thread.Start ();
thread.Join (1000);
Assert.That (result, Is.EqualTo ("Some label"));
}
The test passes, but if I set a breakpoint in the InvokeEx method (and not in the call), I see that Control.InvokeRequired is incorrect, which leads to functions .
In addition, now the test failed because the result is not set.
, , func ( ), .
, , , unit test? ?