Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
How to trigger OnMouseOver/OnMouseExit in a Unity test script?

I created a script for my Unity game managing the hover state of the gameobject the script is attached to

public sealed class HoverStateController : MonoBehaviour
{
    private void OnMouseOver()
    {
        UpdateEntityHoverState(true);
    }

    private void OnMouseExit()
    {
        UpdateEntityHoverState(false);
    }

    private void UpdateHoverState(bool hovered)
    {
         // ... modify the state ...
    }
}

I know that I should not try to test private methods because there is no need for it, I can check the result of the public service (Should I test private methods or only public ones?).

The thing is that the logic in UpdateHoverState is testable very easily. So I could

  • not test it, because it's private
  • make the method public and test it, but access modifiers should be as limited as possible, so this breaks a convention (Access Modifiers - what's the purpose?)
  • perform a OnMouseOver and OnMouseExit by triggering something

I would prefer the last one but I don't know if Unity offers a way to trigger those methods. Is there a way to create a mock which calls them internally?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Firstly OnMouseOver() is called in every frame you probably want to use OnMouseEnter() instead.

Unity's scripting is not pure OO feel free to violate laws whenever you feel like it needs to be. E.g. every Unity message can be public, protected, private etc. so you could just make the access modifier public and trigger it yourself.

Also you can use reflection to call private methods since you only want to test the UpdateHoverState() method's inner logic this might be better. However reflection is vulnerable to refactors (you need the name of the method in the form of a string). For accessing private methods via a cool extension method using reflection see this answer.

And you can also use preprocessor symbols in order to make a method public when testing or to provide additional information about the class. Unity preprocessors

#if TESTING
// public wrapper method when testing
public void UpdateHoverStateTestHelper(bool hovered)
{
    UpdateHoverState(hovered);
}
#endif
void UpdateHoverState(bool hovered)
{
    // ... modify the state ...
}
#if TESTING || UNITY_EDITOR
// Now we will know the method name even when it gets renamed (using refactoring tools)
static readonly string UpdateHoverStateMethod = nameof(UpdateHoverState);
#endif
void UpdateHoverState(bool hovered)
{
    // ... modify the state ...
}
#if TESTING || UNITY_EDITOR
// nameof is accessed at compile time so the string can be const too
const string UpdateHoverStateMethod = nameof(UpdateHoverState);
#endif
void UpdateHoverState(bool hovered)
{
    // ... modify the state ...
}

The cool thing about these is that whenever you are building for your target platform these codes will be stripped (assuming you unset the TESTING preprocessor)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!