Last active
December 20, 2015 10:39
-
-
Save blair55/6116950 to your computer and use it in GitHub Desktop.
New Tricks from an Old Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ScoreChecker | |
{ | |
public IPlayer GetScoredPlayer(IPlayer player) | |
{ | |
if (player.Score > 8) | |
{ | |
return new HighScorePlayer(player); | |
} | |
if (player.Score > 2) | |
{ | |
return new LowScorePlayer(player); | |
} | |
throw new ScoreTooLowException(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class WhenIGetScoredPlayer : GivenA<ScoreChecker> | |
{ | |
protected readonly Mock<IPlayer> MockPlayer = new Mock<IPlayer>(); | |
protected IPlayer Result = null; | |
protected override void When() | |
{ | |
base.When(); | |
Result = Target.GetScoredPlayer(MockPlayer.Object); | |
} | |
} | |
public class GivenThePlayerHasAScoreGreaterThan8 : WhenIGetScoredPlayer | |
{ | |
protected override void Given() | |
{ | |
base.Given(); | |
MockPlayer.Setup(m => m.Score).Returns(9); | |
} | |
[Then] | |
public void TheResultIsAHighScorePlayer() | |
{ | |
Assert.IsInstanceOf<HighScorePlayer>(Result); | |
} | |
} | |
public class GivenThePlayerHasAScoreGreaterThan2 : WhenIGetScoredPlayer | |
{ | |
protected override void Given() | |
{ | |
base.Given(); | |
MockPlayer.Setup(m => m.Score).Returns(3); | |
} | |
[Then] | |
public void TheResultIsALowScorePlayer() | |
{ | |
Assert.IsInstanceOf<LowScorePlayer>(Result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GivenThePlayerHasAScoreLowerThan2 : WhenIGetScoredPlayer | |
{ | |
protected override void Given() | |
{ | |
base.Given(); | |
MockPlayer.Setup(m => m.Score).Returns(1); | |
} | |
protected override void When() | |
{ | |
// prevent base.When() from being called | |
// and throwing an exception too early! | |
} | |
[Then] | |
public void AScoreTooLowExceptionIsThrown() | |
{ | |
Assert.Throws<ScoreTooLowException>(() => base.When()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment