1/10/2011 4:29:11 AM
 grega Posts: 6
|
Hi,
I stumbled upon T# searching for some information about unit testing and couldn't stop reading about it. I absolutely love the simplicity of it. I read through the documentation and I am very impressed. I am thinking about converting all out test into T#. However there is one thing I wanted to ask. Is there a way to SIMPLY test asynchronous code. Below is an example to show you what I mean and to show how I would do it using MS testing framework.
Let's say I have a class which uses threading:
public class ThreadedClass { public void Start() { new Thread(ThreadedMethod).Start(); } private void ThreadedMethod() { Thread.Sleep(1000); if (Event != null) Event(this, new EventArgs()); } public event EventHandler Event; }
I would test it like this:
[TestMethod] public void TestMethod1() { ManualResetEvent mre = new ManualResetEvent(false); bool eventFired = false;
ThreadedClass threadedClass = new ThreadedClass(); threadedClass.Event += (Object sender, EventArgs e) => { eventFired = true; mre.Set(); }; threadedClass.Start();
mre.WaitOne(2000, false);
Assert.IsTrue(eventFired, "Event wasn't fired"); }
In T# I could probably test it using "assert raised" and using Thread.Sleep. But I hope there must be a simpler way of doing this with T#. Something like this maybe:
test ThreadedClass { ThreadedClass threadedClass = new ThreadedClass(); runtest threadedClass.Start(); assert raised threadedClass.Event within 2000; }
|
|
|
0
• permalink
|
1/10/2011 9:10:20 AM
 Ludovic Dubois Administrator Posts: 159
|
Thanks for your comment!
You have a very good idea. I will see what I can do... soon...
|
|
|
0
• permalink
|
3/4/2011 4:38:18 PM
 Ludovic Dubois Administrator Posts: 159
|
I just release Visual T# v1.10 that enables you to test asynchronous events.
Have good tests
|
|
|
0
• permalink
|