I actually wrote this Controller Method in Ruby, but I will translate it to C# style. This is why I finally began to like TDD. Notice that there is a single call to Model (Movie) from Controller Method.
{
Movie movie = Movie.Find(id);
if (movie.director == null || movie.director.size == 0)
{
String text = movie.title + " has no director info";
this.Flash(FlashType.Warning, text);
return RedirectToAction("HomePage");
}
this.movies = Movie.where("director = " + movie.director);
}
{
try
{
this.movies = Movie.FindByDirector(id);
}
catch (NoDirectorException e)
{
this.Flash(FlashType.Warning, e.description);
return RedirectToAction("HomePage");
}
}
Without TDD
String ByDirector(int id){
Movie movie = Movie.Find(id);
if (movie.director == null || movie.director.size == 0)
{
String text = movie.title + " has no director info";
this.Flash(FlashType.Warning, text);
return RedirectToAction("HomePage");
}
this.movies = Movie.where("director = " + movie.director);
}
With TDD
String ByDirector(int id){
try
{
this.movies = Movie.FindByDirector(id);
}
catch (NoDirectorException e)
{
this.Flash(FlashType.Warning, e.description);
return RedirectToAction("HomePage");
}
}
2 comments:
Asim could not quite get it completely. Yes I can see you introduced the exceptions to ensure that either the tests run successfully or they notify but still need some more explanation.
Single responsibility Principle is what the code is signifying.. Controller should talk to Model
Controller method doesn’t need to know the internals of DB to facilitate view.. e.g. Movie.name.. obviously the code written with TDD could be written without TDD but people generally don’t.. and I probably had never without TDD…
Post a Comment