2
votes

I'm writing a unit test code for a GameBoard object that's part of a project to make the card game "Dominion" in C#. I'm using rhino mocks to test a method that runs a while(! GameIsOver). Inside, the next player is gotten, then called on to take their turn. A lot of this code is not implemented yet (player.takeTurn() is empty for example).

In my unit test, I'm using Rhino Mocks to override the method GameIsOver to return false several times, then return true to end the test at the expected time, but I need to use CallOriginalMethod to call the method I'm testing in GameBoard. The lines that use that method are telling me that CallOriginalMethod is obsolete and requires "original call options" which I have been unable to find any documentation on.

[TestMethod]
public void TestTurnOrderUsingMocks()
{
    MockRepository mocks = new MockRepository();
    GameBoard fakeBoard = mocks.DynamicMock<GameBoard>();
    Player p1 = mocks.DynamicMock<Player>();
    Player p2 = mocks.DynamicMock<Player>();
    Dictionary<Card, int> cards = GetTestDeck();

    using (mocks.Ordered())
    {
        fakeBoard.PlayGame();
        for (int i = 0; i < 10; i++)
        {
            p1.TakeTurn();
            p2.TakeTurn();
        }
    }
    Expect.Call(fakeBoard.AddPlayer(p1)).CallOriginalMethod();
    Expect.Call(fakeBoard.AddPlayer(p2)).CallOriginalMethod();
    Expect.Call((()=>fakeBoard.PlayGame())).CallOriginalMethod();
    Expect.Call(fakeBoard.GameIsOver()).Repeat.Times(20).Return(false);
    Expect.Call(fakeBoard.GameIsOver()).Return(true);
    mocks.ReplayAll();
    fakeBoard.PlayGame();
    mocks.VerifyAll();
}

public Boolean AddPlayer(Player p)
{
    if (turnOrder.Contains(p))
    {
        Console.WriteLine("that player has already been added!");
        return false;
    }
    turnOrder.Enqueue(p);
    return true;
}

public virtual void PlayGame()
{
    while (!GameIsOver())
    {
        NextPlayer().TakeTurn();
    }
}
1
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not".John Saunders

1 Answers

4
votes

You have to call "CallOriginalMethod" method with "OriginalCallOptions" enumeration.(By the way, you use RhinoMocks's old API...)

Change your calls to:

        fakeBoard.Stub(x => x.AddPlayer(x => x.AddPlayer(Arg<Player>.Is.NotNull)))
                 .CallOriginalMethod(OriginalCallOptions.NoExpectation)
                 .Return(true);

One more thing, the method "PlayGame" must be a virtual method(to apply this behavior...)