I caught a whiff of a code-smell emanating 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;
foreach(var wheel in _car.Wheels)
{
wheel.Rolling += WheelRollingHandler;
}
}
}
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 its Wheels
are initialised correctly.
I wondered what pointers people here might give me?