I just need to unit test some methods like this:
public void start()
{
myThread.Start();
}
public void stop()
{
myThread.Abort();
}
How can i do that? I may call start method and check for IsAlive==true but then thread keeps working in background and calls its own method. Another way is I call start and then stop immediately but it's not neat cause it would be like I'm testing two things. It's so simple but arghh!
The trick is to separate the asynchronous behavior from your logic. Observe that your actual logic is synchronous, and that the asynchronous part is simply some "waiting time" in between.
The book xUnit Test Patterns describes it well. It's even a pattern - Humble Object. It's explained there way better than I can.
Use thread synchronization
and wait handles
to wait on running threads.
Thread Synchronization (C# Programming Guide)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With