public void DefendAgainstAttack_SuperMode_ShouldDecrementHealthWithHalfAttackStrength() { //Arrange int health = Random.Next(50, 100); IHero hero = new HeroBuilder().WithSuperModeLikeliness(1).WithHealth(health).Build(); int attackStrength = Random.Next(1, hero.Health); //Act hero.DefendAgainstAttack(attackStrength); //Assert Assert.That(hero.Health, Is.EqualTo(health - attackStrength / 2)); }
private static void AssertSuperModeLikelinessForDefend(float superModeLikeliness) { int numberOfDefends = 200; int numberOfNormalDefends = 0; int numberOfSuperDefends = 0; for (int i = 0; i < numberOfDefends; i++) { int startHealth = Random.Next(60, 101); int attackStrength = Random.Next(2, 20); if (attackStrength % 2 != 0) { attackStrength += 1; } IHero hero = new HeroBuilder().WithSuperModeLikeliness(superModeLikeliness).WithHealth(startHealth).Build(); hero.DefendAgainstAttack(attackStrength); int expectedHealthNormal = startHealth - attackStrength; int expectedHealthSuperMode = startHealth - attackStrength / 2; Assert.That(hero.Health, Is.EqualTo(expectedHealthNormal).Or.EqualTo(expectedHealthSuperMode), $"After defending an attack of strength {attackStrength} with an initial health of {startHealth}, " + $"the health of the hero should be {expectedHealthNormal} or {expectedHealthSuperMode}."); if (hero.Health == expectedHealthNormal) { numberOfNormalDefends++; } else { numberOfSuperDefends++; } } string staticRandomTip = "Tip: make sure all 'Hero' instances use the same 'Random' instance by declaring a static field like this: 'private static Random Random = new Random();'"; Assert.That(numberOfNormalDefends, Is.GreaterThan(0), $"Out of {numberOfDefends} defends, no normal defend happened. That is not random enough. {staticRandomTip}"); Assert.That(numberOfSuperDefends, Is.GreaterThan(0), $"Out of {numberOfDefends} defends, no supermode defend happened. That is not random enough. {staticRandomTip}"); double actualSuperMode = numberOfSuperDefends / (double)numberOfDefends; Assert.That(actualSuperMode, Is.EqualTo(superModeLikeliness).Within(0.15), $"After {numberOfDefends} defends the supermode likeliness seems to be around {Convert.ToInt32(actualSuperMode * 100)}%. " + $"It should be around {Convert.ToInt32(superModeLikeliness * 100)}%. {staticRandomTip}"); }