Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is what I've done in these situations: Create a service that takes the delegate and executes it (expose this via a simple interface, inject where you call stuff asynchronously). </p> <p>In the real service it will execute with BeginInvoke and thus asynchronously. Then create a version of the service for testing that invokes the delegate synchronously. </p> <p>Here's an example:</p> <pre><code>public interface IActionRunner { void Run(Action action, AsyncCallback callback, object obj); void Run&lt;T&gt;(Action&lt;T&gt; action, T arg, AsyncCallback callback, object obj); void Run&lt;T1, T2&gt;(Action&lt;T1, T2&gt; action, T1 arg1, T2 arg2, AsyncCallback callback, object obj); void Run&lt;T1, T2, T3&gt;(Action&lt;T1, T2, T3&gt; action, T1 arg1, T2 arg2, T3 arg3, AsyncCallback callback, object obj); void Run&lt;T1, T2, T3, T4&gt;(Action&lt;T1, T2, T3, T4&gt; action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, AsyncCallback callback, object obj); } </code></pre> <p>An Asycnhronous implementation of this service looks like this: </p> <pre><code>public void Run&lt;T&gt;(Action&lt;T&gt; action, T arg, AsyncCallback callback, object obj) { action.BeginInvoke(arg, callback, obj); } </code></pre> <p>A Synchronous implementation of this service looks like this: </p> <pre><code>public void Run&lt;T&gt;(Action&lt;T&gt; action, T arg, AsyncCallback callback, object obj) { action(arg); } </code></pre> <p>When you are unit testing, if you're using something like RhinoMocks and the AutoMocking container, you can swap in your Synchronous ActionRunner: </p> <pre><code>_mocks = new MockRepository(); _container = new AutoMockingContainer(_mocks); _container.AddService(typeof(IActionRunner), new SyncActionRunner()); _container.Initialize(); </code></pre> <p>The ActionRunner doesn't need to be tested; it's just a thin veneer over the method call. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload