What Do You Think About This Smelly Test?
Posted
by
panamack
on Programmers
See other posts from Programmers
or by panamack
Published on 2011-06-22T00:13:20Z
Indexed on
2011/06/22
0:31 UTC
Read the original article
Hit count: 201
code-smell
I caught a whiff of a smell eminating from one of my tests, in a scenario akin to the following:
[TestFixture]
public void CarPresenterTests{
[Test]
public void Throws_If_Cars_Wheels_Collection_Is_Null(){
IEnumerable<Wheels> wheels = null;
var car = new Car(wheels);
Assert.That(
()=>new CarPresenter(car),
Throws.InstanceOf<ArgumentException>()
.With.Message.EqualTo("Can't create if cars wheels is null));
}
}
public class CarPresenter{
public CarPresenter(Car car)
{
if(car.Wheels == null)
throw new ArgumentException("Can't create if cars wheels is null);
_car = car;
_car.Wheels.Rolling += WheelsRollingHandler;
}
}
I was struggling to describe what the problem is except that it seems wrong that a CarPresenter should attempt to dictate to a Car whether or not it's Wheels are initialised correctly.
I wondered what pointers people here might give me?
© Programmers or respective owner