Writing an Application using TDD (Part 3) First Detail

First post
Next post
Previous post

Since the previous post, I applied the suggestions in comments from @MartinSalias and @theHumanFlag, thanks!. First, I renamed Genre class to Subject, using the refactoring features of Visual Studio:

public class Subject
{
    public string Name { get; set; }
}

Second, now the controller receives and process an IEnumerable<Subject>:

public class SubjectController : Controller
{
    private IEnumerable<Subject> subjects;
    public SubjectController()
    {
    }
    public SubjectController(IEnumerable<Subject> subjects)
    {
        this.subjects = subjects;
    }
    public ActionResult Index()
    {
        return View(subjects);
    }
}

I made the change with confidence, thanks to the test.

In this new step, I added an action: to get a Subject by Id. A new property Id in Subjec:

public class Subject
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I wrote the test:

[TestMethod]
public void GetSubjectForDetail()
{
    IEnumerable<Subject> subjects = new List<Subject>()
    {
        new Subject() { Id = 1, Name = "Mathematics" },
        new Subject() { Id = 2, Name = "Physics" },
        new Subject() { Id = 3, Name = "Biology" },
        new Subject() { Id = 4, Name = "Literature" }
    };
    SubjectController controller = new SubjectController(subjects);
    ActionResult result = controller.Details(1);
    Assert.IsNotNull(result);
    Assert.IsInstanceOfType(result, typeof(ViewResult));
    ViewResult viewResult = (ViewResult)result;
    Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(Subject));
    Subject model = (Subject)viewResult.ViewData.Model;
    Assert.AreEqual(1, model.Id);
    Assert.AreEqual("Mathematics", model.Name);
}

The test didn’t compile. I created the action:

public ActionResult Details(int id)
{
    return View();
}

The test compile, but its result was red. Now, go for the green light:

public ActionResult Details(int id)
{
    var model = this.subjects.Where(s => s.Id == id).FirstOrDefault();
    return View(model);
}

I had two tests, that give a list of Subjects to the actions. I refactored the test to separate the creation of such list. I extracted the method:

private static IEnumerable<Subject> GetSubjects()
{
    IEnumerable<Subject> subjects = new List<Subject>()
    {
        new Subject() { Id = 1, Name = "Mathematics" },
        new Subject() { Id = 2, Name = "Physics" },
        new Subject() { Id = 3, Name = "Biology" },
        new Subject() { Id = 4, Name = "Literature" }
    };
    return subjects;
}

Now, the test code is:

[TestMethod]
public void GetSubjectInDetail()
{
    IEnumerable<Subject> subjects = GetSubjects();
    SubjectController controller = new SubjectController(subjects);
    ActionResult result = controller.Details(1);
    Assert.IsNotNull(result);
    Assert.IsInstanceOfType(result, typeof(ViewResult));
    ViewResult viewResult = (ViewResult)result;
    Assert.IsInstanceOfType(viewResult.ViewData.Model, typeof(Subject));
    Subject model = (Subject)viewResult.ViewData.Model;
    Assert.AreEqual(1, model.Id);
    Assert.AreEqual("Mathematics", model.Name);
}

All tests in green!

As usual, the code is in my AjCodeKatas Google Project, under trunk/AppTdd/Step02)

Next steps: more actions, integrate views.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

2 thoughts on “Writing an Application using TDD (Part 3) First Detail

  1. Pingback: Writing An Application Using TDD (Part 2) First List « Angel “Java” Lopez on Blog

  2. Pingback: Writing An Application Using TDD (Part 4) Update and Insert « Angel “Java” Lopez on Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s