Ayende @ Rahien

Unnatural acts on source code

An example of bad test, part 1

Let us take a look at this test. When I saw it, the test cried out in pain to me: "Help me, I am so overworked."

image

This test is doing far too much. I drew the battle line on the test, just to give you a clear indication on what is going on.

image

What we are asserting are things that have nothing to do with what the test is supposed to test.This is also the classic "A test should have a single assertion" example, I think.

The test pass by side effect. It will actually be a problem down the line.

Comments

Rob Conery
08/17/2008 03:03 AM by
Rob Conery

Ayende this is a spike, as I mentioned about 5 times on my blog and in the video. I understand the issue you're having here - I just wanted to see if it would work. It did...

Rob Conery
08/17/2008 03:11 AM by
Rob Conery

Also - it's worth mentioning that I have no idea how to unit test something that is fired in an asynch way. In other words, WWF uses the WorflowRuntime, which is off on its own thread. I have a race condition in this code but believe it or not - the result came in :) and I was happy.

Now that said - what I could do is start and then stop the runtime - wait for it, and then see what the status of the order is... thoughts?

Ayende Rahien
08/17/2008 03:19 AM by
Ayende Rahien

Rob,

Just to clarify.

Spike code has no standards, agreed.

It just turned out to be a good example to show bad testing practices.

Ayende Rahien
08/17/2008 03:20 AM by
Ayende Rahien

Testing async code is tough.

ManualResetEvent is the common case.

IVR
08/18/2008 09:34 AM by
IVR

Well, it looks like C#, is it?

Comments have been closed on this topic.