public void DoWorkShouldCallWorkerThreadWorkItemStartingBeforeProcessingWorkItem() { WorkerThreadManagerStub workerThreadManager = WorkerThreadManagerStub.Create(); ManualResetEvent manualResetEvent = new ManualResetEvent(false); IWorkItemQueue workItemQueue = Substitute.For <IWorkItemQueue>(); IWorkItem workItem = Substitute.For <IWorkItem>(); workItemQueue.Dequeue().Returns(workItem); Exception otherThreadsException = null; WorkerThread workerThread = new WorkerThread(workerThreadManager, workItemQueue, "Test", false); workerThreadManager.WorkerThreadWorkItemStarting += (sender, args) => { try { Assert.AreEqual(workItem, args.WorkItem); workItem.DidNotReceive().DoWork(); } catch (Exception ex) { otherThreadsException = ex; } workerThread.Stop(); manualResetEvent.Set(); }; workerThread.Start(); manualResetEvent.WaitOne(500); workerThread.Stop(); Wait.While(() => workerThread.Thread.IsAlive, 300); Assert.AreEqual(false, workerThread.Thread.IsAlive); if (otherThreadsException != null) { throw otherThreadsException; } }