I usually solve such problems by encapsulating the global state with an abstract class or interface that can be staged and mocked. Then, instead of directly accessing the global state, I add an instance of my abstract class or interface to the code that uses it.
This allows me to mock global behavior and makes it so that my tests are not dependent on or perform this unrelated behavior.
Here you can do it.
public interface IContext { void Post(SendOrPostCallback d, Object state); } public class SynchronizationContextAdapter : IContext { private SynchronizationContext _context; public SynchronizationContextAdapter(SynchronizationContext context) { _context = context; } public virtual void Post(SendOrPostCallback d, Object state) { _context.Post(d, state); } } public class SomeClass { public SomeClass(IContext context) { _context = context; } void _tracker_IndexChanged(object sender, IndexTrackerChangedEventArgs e) { _context.Post(o => _view.SetTrackbarValue(_tracker.Index), null); }
You can then make fun of or IContext
so you donβt have to worry about streaming, and you can use a simple implementation that the delegate just does.
If I wrote unit tests to mock it, I would also write integration tests at a higher level that did not scoff at it, but had less granular checks.
source share