I'm writing a chess engine as an exercise in TDD, using C#, NUnit & RhinoMocks.
The first class I want to write is the Game class, which will have a parameterless constructor that will create all chess pieces in their opening positions as a collection of 64-bit bitmaps (one bitmap for the white rooks, one for the white knights etc.)
There will also be a GetPieces() method that returns a collection of IPiece objects, which it generates from the bitmaps. Each IPiece will have a name and coordinates.
So in the test I create a new Game object, call GetPieces() and check the collection for all the expected pieces and their positions.
After creating the test I created the Game class and the IPiece interface but no others, as this is intended to be a true unit test (at least as I understand the definition of a unit test).
So now I have a compiling but failing test. Great.
Now to write the GetPieces() method. Ooops! I have to create some new classes that implement IPiece and instantiate them here, making Game dependent on these new classes. Bad OO design.
OK, I'll delegate piece creation off to a factory class. This can be dependent on piece classes because it's a factory, but Game shouldn't be dependent on the factory so I'll inject it in the Game constructor. Now for my test I can inject a mock factory into
Game.
Now my mock factory (or should it be a stub?) will return the piece collection to Game, which will return it to my test, and my test will Assert that all pieces are present and correct.
Now all the test does is confirm that the factory is calling the appropriate method. It doesn't actually prove that the Game constructor is doing it's job.
Without publicly exposing the bitmaps or adding unwanted dependencies, how can I create a true unit test that confirms the work of the Game constructor?
c0d3r
Member
61 Points
20 Posts
True unit testing
Oct 09, 2008 03:57 AM|LINK
The first class I want to write is the Game class, which will have a parameterless constructor that will create all chess pieces in their opening positions as a collection of 64-bit bitmaps (one bitmap for the white rooks, one for the white knights etc.)
There will also be a GetPieces() method that returns a collection of IPiece objects, which it generates from the bitmaps. Each IPiece will have a name and coordinates.
So in the test I create a new Game object, call GetPieces() and check the collection for all the expected pieces and their positions.
After creating the test I created the Game class and the IPiece interface but no others, as this is intended to be a true unit test (at least as I understand the definition of a unit test).
So now I have a compiling but failing test. Great.
Now to write the GetPieces() method. Ooops! I have to create some new classes that implement IPiece and instantiate them here, making Game dependent on these new classes. Bad OO design.
OK, I'll delegate piece creation off to a factory class. This can be dependent on piece classes because it's a factory, but Game shouldn't be dependent on the factory so I'll inject it in the Game constructor. Now for my test I can inject a mock factory into Game.
Now my mock factory (or should it be a stub?) will return the piece collection to Game, which will return it to my test, and my test will Assert that all pieces are present and correct.
Now all the test does is confirm that the factory is calling the appropriate method. It doesn't actually prove that the Game constructor is doing it's job.
Without publicly exposing the bitmaps or adding unwanted dependencies, how can I create a true unit test that confirms the work of the Game constructor?
Many thanks
tdd mocking rhinomocks nunit