Hey Gamers & Game Makers!
I’ve been writing a few test for my new game package and trying to figure out why WaitForSeconds(1) is not waiting for 1 second, hence causing my test to fail.
I would expect WaitForSeconds(1); to cause Time.DeltaTime to move 1 second so my Skill::Recharge method would recharge for 1s…
public virtual void Recharge()
{
// Only recharge if we are not at max charges
if (currentCharges < maxCharges)
{
// Advance the recharge timer... accounting for the recharge booster also.
rechargeTimer += Time.deltaTime * rechargeBooster;
if (rechargeTimer >= rechargeValue)
{
currentCharges++;
rechargeTimer -= rechargeValue;
Debug.Log($"{name} charge gained! Charges available: {currentCharges}");
}
}
else
{
rechargeTimer = 0f;
}
}
Anything obvious I am doing wrong?
[UnityTest]
public IEnumerator Skill_WhenRechargedWithBooster_CanUse()
{
// Arrange
const float booster = 2;
Skill skill = ScriptableObject.CreateInstance<Skill>();
skill.Initialize(1, 1, 2, booster);
// Act
skill.Use();
yield return new WaitForSeconds(1);
skill.Recharge();
// Assert
Assert.True(skill.CanUse());
}