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.

ZebraGameTest/ZebraProblemTest.cs
using NUnit.Framework;

namespace ZebraGameProblemTests
{
    [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 to the ZebraGame project.

ZebraGameProblem/ZebraProblem.cs
namespace ZebraGameProblem
{
    public class ZebraProblem
    { }
}

Add a reference to the ZebraGame project in the ZebraGame.Test project.

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

ZebraGameTest/ZebraProblemTest.cs
using NUnit.Framework;

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

            Assert.That( zebraProblem.Houses.Count,
                         Is.EqualTo( 5 ),
                         "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.

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

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

We created a public property to hold a list of houses. Now we need a House class.

ZebraGameProblem/House.cs
namespace ZebraGameProblem
{
    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.

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

namespace ZebraGameProblem
{
    public class ZebraProblem
    {
        public ZebraProblem()
        {
            const int houseMax = 5;

            Houses = new List<House>( houseMax );

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

        public List<House> Houses { get; }
    }
}

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

1 test passed