Say the right thing – don’t just do the right thing.
Often, I see tests like the following:
public void SuccessulUpdateOfCarDeatilsShouldReturnSuccessResponse()
{
CarController controller = new CarController()
controller.UpdateCarDetails(0, string.Empty, string.Empty, string.Empty)
Assert.AreEqual(200, controller.Response)
}
- They treat tests as second-class citizens
- They have no respect for me, the unknown reader of the test
public class CarController{
...
public void UpdateCarDeatils(int carId, string carRegistration, string colour, string engineIdentificationNumber)
{
...
}
}
public void SuccessulUpdateOfCarDeatilsShouldReturnSuccessResponse()
{
CarController controller = new CarController()
controller.UpdateCarDetails(42, "car registration", "colour", "engineIdentificationNumber")
Assert.AreEqual(200, controller.Response)
}
Posted: September 7th, 2008 under Testing.
Comments
Comment from Joe
Time May 18, 2010 at 5:43 pm
> *Even with Intelli-J or Resharper,
> there is work involved to understand
> these values
Doh! Speaking of Resharper, some marketing guy from M$ called me a few days ago and asked how I liked Visual Studio 2008. I told him that it the stock/ out-of-the-box refactorings in VS suck and that they should either include Resharper functionality in VS (for free) or they should copy Eclipse’s refactorings. OMG, VS 2008 doesn’t even let you refactor the basics like method return types and parameter names.
Comment from Joe
Time May 18, 2010 at 5:33 pm
Sorry, I didn’t realize you had a new blog. I’m reposting my comment from blogger:
I’m totally with you on this one, Sarah. I usually practice Test First Development. I like to go one step further and give the parameters a name, to further clarify my intentions. For example:
public void SuccessulUpdateOfCarDeatilsShouldReturnSuccessResponse()
{
CarController controller = new CarController();
int dummyCarId = 42;
string carRegistration = “car registration”;
string colour = “colour”;
string engineIdentificationNumber = “engineIdentificationNumber”;
controller.UpdateCarDetails(dummyCarId, carRegistration, colour, engineIdentificationNumber);
Assert.AreEqual(HttpStatusCode.OK, controller.Response);
}
Giving the parameters a name is especially beneficial when the method under test has many overloads. Note that I also changed the expected value 200 to HttpStatusCode.OK.
Keep up the good work.