I have a unit test that relies on a random dice roll. I roll a 20 sided die and if the value is 20 it counts as a critical hit.
What I'm doing right now is rolling the 20 sided die up to 300 times. If any one of those rolls is a 20 then I know I had a critical hit.
Here's what the code looks like:
public class DiceRoll
{
public int Value { get; set; }
public bool IsCritical { get; set; }
// code here that sets IsCritical to true if "Value" gets set to 20
}
[Test]
public void DiceCanRollCriticalStrikes()
{
bool IsSuccessful = false;
DiceRoll diceRoll = new DiceRoll();
for(int i=0; i<300; i++)
{
diceRoll.Value = Dice.Roll(1, 20); // roll 20 sided die once
if(diceRoll.Value == 20 && diceRoll.IsCritical)
{
IsSuccessful = true;
break;
}
}
if(IsSuccessful)
// test passed
else
// test failed
}
Although the test does exactly what I want it to I can't help but feel like I'm doing something wrong.
On a related note, the DiceRoll class has other information in it as well but my question is specifically about looping in a unit test, so I left it out to make it more clear