Last active
March 6, 2017 16:48
-
-
Save charlieamat/0556f5cb5ccb36395dd13b6f07efdde0 to your computer and use it in GitHub Desktop.
Why Is Unit Testing So Hard? - Example 2 Refactored Tests
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 Weapon _weapon; | |
private IPowerup _powerup; | |
[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 < 5; i++) | |
{ | |
var player = new Player(_weapon); | |
for (var j = 0; j < i; j++) | |
{ | |
player.Powerups.Add(_powerup); | |
} | |
Assert.AreEqual(CalculateDamage(player.Powerups), player.Damage); | |
} | |
} | |
private double CalculateDamage(IEnumerable<IPowerup> powerups) | |
{ | |
return _weapon.Damage * powerups.Sum(p => p.Multiplier); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment