Zebra Game Kata Part 3 - Houses

The first statement in the Zebra Problem says, "There are five houses".

Let's write a test that the Zebra class contains a list of five house objects.

ZebraGame.Test/ZebraProblemTest.cs
using NUnit.Framework;

namespace ZebraGame.Test
{
    [TestFixture]
    public class ZebraProblemTest
    {
        [Test]
        public void There_Are_Five_Houses()
        {
            var zebraProblem = new ZebraProblem();
        }
    }
}

At this point we cannot even run the test because the compiler is returning an error. We need to add a ZebraProblem class. Put your cursor on ZebraProblem and press CTRL+. to access the Generate from Usage menu. Select "Generate new type…".

Control Dot on ZebraProblem

In the Generate New Type dialog, select the ZebraGame project and click Ok.

Generate New Type

Three things just happened.

  1. The ZebraProblem class was added to the ZebraGame project.
  2. A reference to the ZebraGame project was added to the ZebraGame.Test project.
  3. "using ZebraGame;" was added to the top of the ZebraProblemTest class. This using statement is not needed. You can remove it.

Let's assert that there are five houses in the ZebraProblem class.

ZebraGame.Test/ZebraProblemTest.cs
using NUnit.Framework;

namespace ZebraGame.Test
{
    [TestFixture]
    public class ZebraProblemTest
    {
        [Test]
        public void There_Are_Five_Houses()
        {
            var zebraProblem = new ZebraProblem();

            Assert.AreEqual(5, zebraProblem.Houses.Count,
                "There must be five houses.");
        }
    }
}

Again we have a compile-time error. We are counting the Houses property of the ZebraProblem class before the property exists. Let's switch over to the ZebraProblem class and create the list of houses.

ZebraGame/ZebraProblem.cs
using System.Collections.Generic;

namespace ZebraGame
{
    public class ZebraProblem
    {
        public List<House> Houses { get; set; }
    }
}

We created a public property to hold a list of houses. Now we need a House class. CTRL+. on the House and add a class.

ZebraGame/House.cs
namespace ZebraGame
{
    public class House
    {
    }
}

Now that we can compile, let's run the test.

1 test failed "System.NullReferenceException"

Although we have a property, it is null. In the ZebraProblem constructor, we need to populate the list with five houses.

ZebraGame/ZebraProblem.cs
using System.Collections.Generic;

namespace ZebraGame
{
    public class ZebraProblem
    {
        public List<House> Houses { get; set; }

        public ZebraProblem()
        {
            const int houseMax = 5;

            Houses = new List<House>(houseMax);

            for (int i = 0; i < houseMax; i++)
            {
                Houses.Add(new House());
            }
        }
    }
}

We now have 5 houses. Let's run the tests again.

1 test passed