public void ChangesDirectionIfBlocked() { // Arrange var gnomeMovement = MockRepository.GenerateMock <IGnomeMovement>(); var gnome = new Gnome(1, 1, 1, gnomeMovement); gnomeMovement.Stub(x => x.IsFrozen(gnome)).Return(false); Direction?firstDirectionChosen = null; var firstDirectionCount = 0; var otherDirectionCount = 0; gnomeMovement.Stub(x => x.TryMove(Arg <Gnome> .Is.Anything, Arg <Direction> .Is.Equal(Direction.North))) .Do((Func <IGnome, Direction, bool>)((g, d) => CheckDifferentDirection(d, ref firstDirectionChosen, ref firstDirectionCount, ref otherDirectionCount))); gnomeMovement.Stub(x => x.TryMove(Arg <Gnome> .Is.Anything, Arg <Direction> .Is.Equal(Direction.East))) .Do((Func <IGnome, Direction, bool>)((g, d) => CheckDifferentDirection(d, ref firstDirectionChosen, ref firstDirectionCount, ref otherDirectionCount))); gnomeMovement.Stub(x => x.TryMove(Arg <Gnome> .Is.Anything, Arg <Direction> .Is.Equal(Direction.South))) .Do((Func <IGnome, Direction, bool>)((g, d) => CheckDifferentDirection(d, ref firstDirectionChosen, ref firstDirectionCount, ref otherDirectionCount))); gnomeMovement.Stub(x => x.TryMove(Arg <Gnome> .Is.Anything, Arg <Direction> .Is.Equal(Direction.West))) .Do((Func <IGnome, Direction, bool>)((g, d) => CheckDifferentDirection(d, ref firstDirectionChosen, ref firstDirectionCount, ref otherDirectionCount))); // Act gnome.Move(); gnome.Move(); // Assert Assert.AreEqual(1, firstDirectionCount); Assert.AreEqual(1, otherDirectionCount); }
public void InitiallyMovesInRandomDirection() { // Arrange const int timesRun = 10000; var gnomeMovement = MockRepository.GenerateMock <IGnomeMovement>(); gnomeMovement.Stub(x => x.IsFrozen(Arg <Gnome> .Is.Anything)).Return(false); var northCount = 0; var eastCount = 0; var southCount = 0; var westCount = 0; gnomeMovement.Stub(x => x.TryMove(Arg <Gnome> .Is.Anything, Arg <Direction> .Is.Equal(Direction.North))) .Do((Func <IGnome, Direction, bool>)((g, d) => { northCount++; return(true); })); gnomeMovement.Stub(x => x.TryMove(Arg <Gnome> .Is.Anything, Arg <Direction> .Is.Equal(Direction.East))) .Do((Func <IGnome, Direction, bool>)((g, d) => { eastCount++; return(true); })); gnomeMovement.Stub(x => x.TryMove(Arg <Gnome> .Is.Anything, Arg <Direction> .Is.Equal(Direction.South))) .Do((Func <IGnome, Direction, bool>)((g, d) => { southCount++; return(true); })); gnomeMovement.Stub(x => x.TryMove(Arg <Gnome> .Is.Anything, Arg <Direction> .Is.Equal(Direction.West))) .Do((Func <IGnome, Direction, bool>)((g, d) => { westCount++; return(true); })); // Act for (var i = 0; i < timesRun; i++) { var gnome = new Gnome(1, 1, 1, gnomeMovement); gnome.Move(); } // Assert var lowerLimit = 2000; var upperLimit = 3000; Assert.AreEqual(timesRun, northCount + eastCount + southCount + westCount); Assert.AreEqual(true, northCount > lowerLimit && northCount < upperLimit); Assert.AreEqual(true, eastCount > lowerLimit && eastCount < upperLimit); Assert.AreEqual(true, southCount > lowerLimit && southCount < upperLimit); Assert.AreEqual(true, westCount > lowerLimit && westCount < upperLimit); }
public void DoesntMoveIfFrozen() { // Arrange var gnomeMovement = MockRepository.GenerateMock <IGnomeMovement>(); var gnome = new Gnome(1, 1, 1, gnomeMovement); gnomeMovement.Stub(x => x.IsFrozen(gnome)).Return(true); // Act gnome.Move(); // Assert gnomeMovement.AssertWasNotCalled(x => x.TryMove(Arg <Gnome> .Is.Equal(gnome), Arg <Direction> .Is.Anything)); }