Last active
March 6, 2017 15:45
-
-
Save charlieamat/4aef496b4a678ee08c1ac7f8b5058b47 to your computer and use it in GitHub Desktop.
Why is Unit Testing So Hard - Example 3
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 Powerup : IPowerup | |
{ | |
public double Multiplier { get; } | |
} | |
public class Player : IPlayer | |
{ | |
private const int MaxPowerups = 3; | |
private Weapon _weapon; | |
public List<IPowerup> Powerups = { get; } = new List<IPowerup>(); | |
public double Damage => _weapon.Damage * Powerups.Take(MaxPowerups).Sum(p => p.Multiplier); | |
} |
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 PlayerTest | |
{ | |
private IPowerup _powerup; | |
private Weapon _weapon; | |
[SetUp] | |
public void Init() | |
{ | |
_weapon = Substitute.For<Weapon>(); | |
_weapon.Damage.Returns(1); | |
_powerup = Substitute.For<IPowerup>(); | |
_powerup.Multiplier.Returns(2); | |
} | |
[Test] | |
public void PowerupDamage() | |
{ | |
for (var i = 1; i < 4; i++) | |
{ | |
var player = new Player(_weapon); | |
for (var j = 0; j < i; j++) | |
{ | |
player.Powerups.Add(_powerup); | |
} | |
Assert.AreEqual(CalculateDamage(_weapon, player.Powerups), player.Damage); | |
} | |
} | |
public double CalculateDamage(Weapon weapon, List<IPowerup> powerups) | |
{ | |
return weapon.Damage * powerups.Sum(p => p.Modifier); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment