What should you test?

I just found myself writing this:

[Test]
public void When_resolving_using_framework_element_will_create_layout_using_resolver()
{
}

The code that I want to test looks something like this:

public void Register(FrameworkElement frameworkElement)
{
	Register(layoutDeocratorResolver.GetLayoutDecoratorFor(frameworkElement));
}

public void Register(ILayout layout)
{
	if (layouts.ContainsKey(layout.Name))
		throw new DuplicateLayoutException("Layout names must be unique. A layout named '" + 
layout.Name + "' already exists."); layouts.Add(layout.Name, layout); }

The problem that I have here is that it looks like I am trying to specify the mechanics of the code under test, and that is a big mistake. I really should not care how it is doing it. I should care about the results. Therefor, I decided to change the test to:

[Test]
public void When_resolving_using_framework_element_will_add_layout_decorator_for_that_element()
{
}

Now what I am testing is behavior, not mechanics.

Tests that validate the mechanics of the code are brittle, and end up as testing shackles.

Print | posted on Monday, December 10, 2007 1:16 AM

Feedback


Gravatar

# re: What should you test? 12/10/2007 3:09 PM alberto

"looks like I am trying to specify the behavior of the code under test, and that is a big mistake."
I think you messed it up a little here, as you then say:
"Now what I am testing is behavior, not mechanics."

You want to specify behaviour, but not implementation.


Gravatar

# re: What should you test? 12/10/2007 6:44 PM Thomas Eyde

I agree with your conclusion that "Tests that validate the mechanics...are brittle".

Unfortunate for me, that's the kinds of tests I ended up with whenever I went for a mocking framework. There must be a secret ingredient somewhere...


Gravatar

# re: What should you test? 12/10/2007 7:54 PM Ayende Rahien

alberto ,
Yes, absolutely.
I fixed the post, thanks for noticing.

Comments have been closed on this topic.