Asserting Myself…

I’ve been pretty heavy into NUnit lately. I’m trying to get better about actually writing the unit tests before (or at least at the same time) that I write the code. It’s really been helpful as I reconfigure my classes and such and I’ve even started to go back and put some tests in older code that I’ve written. Here’s the main issue that I’ve hit right now and I thought I’d throw it out there for comment in case anyone else has run into.

I have a class that is event driven and I wanted to test those events so I wrote the following unit test:

[Test]
public void ChangeStatus_UserUpdate()
{
MyClient client = new MyClient();
client.OnEventFire += new FireEventHandler( OnEventFired );
client.FireEvent();
Assert.AreEqual(1, Assert.Counter);
}

private void OnEventFired( object sender, FireEventArgs e )
{
Assert.AreEqual( 1, e.Value );
}

Looks fairly easy right?? Unfortunately, there are a few factors working against me here. I know that having multiple asserts in a single test is bad but it’s what I’m having to do to get this to test. Anyone with a better idea, please let me know. The second and biggest problem is that Assert.Counter does not seem to reset before each test is called. Not quite sure why but sometimes I get 3, sometimes it’s 2, sometimes it’s 0. Originally, I thought it was when I was running the whole fixture as opposed to just one test but that didn’t pan out. For now I’ll stick to the “if it works, use it” mentality unless something better comes along.

Advertisements

Comments are closed.

%d bloggers like this: