1
\$\begingroup\$

I made a Blender 3D model with animations. Then I used the Animator Controller, and attached it to the model. The Animator Controller has a few Boolean parameter variables like isWaving, isDoing, and isIdle and more variables like that. If, say, a non-idle action is true, then the animation of my model transitions to the next one. I am into test-driven development, but I have never done it with Unity.


I tried writing the following code to test my project:

[SetUp]
public void SetUp()
{
    Crystal = GameObject.FindGameObjectWithTag("CrystalModel");
    CrystalScript = Crystal.GetComponent<CrystalChanPlayer>();
    CrystalAnimator = Crystal.GetComponent<Animator>(); // unity game animator
}

[Test]
public void ifSetAnimationIstodoThenToDoAnimationIsPlayed()
{
    CrystalScript.setAnimationStrategy("todo"); // set strategy
    CrystalScript.playAnimation(); // sets todo boolean to true and idle to false

    // first debug output
    Debug.LogError("todo is " + CrystalAnimator.GetBool("isDoing") + 
        " idle is " + CrystalAnimator.GetBool("isIdle"));

    //second output
    Debug.LogError("current state is " + 
        CrystalAnimator.GetCurrentAnimatorStateInfo(0).IsName("idle"));

    Assert.True(CrystalAnimator.GetCurrentAnimatorStateInfo(0).IsName("todoAnimation"));
}

I received the following output:

"todo is true and idle is false"
"true"


I think this is because Unity isn't really running, and my Animator doesn't get to update based on the variable changes. The first output assures me that todo is set to true and idle is false, hence the Animator should transition that animation to the todoAnimation. Yet, I can't seem to show that, in my tests.

Is there any way to test the Animator class? Are there any design patterns I can use?

\$\endgroup\$
0
\$\begingroup\$

I'm testing my animators by always using animator.Update(0.1f) to make them pass from states to states.

You should try this after your following triggers.

CrystalScript.setAnimationStrategy("todo"); // set strategy
CrystalScript.playAnimation(); // sets todo boolean to true and idle to false
CrystalScript.Update(0.1f);

Some times I also manually call Awake()/Start() on my scripts, as you said, unity is not running at this time so you have to mock the sequencing manually.

Let me know it does any good.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.