Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheSmokingGnu · Jun 10, 2018 at 12:34 AM · monobehaviourtestingawakeunittest

How to mock or skip execution of Awake in playmode unit tests?

I am trying to make my game testable, and i've separated as much logic as i could from MonoBehaviours:

At the editor level, i have the RabbitMonoBehaviour, that get's serialized input from editor and creates Rabbit (pure C# class) from that in it's Awake. The Rabbit is exposed as a public readonly property.

This way, i can mock all the dependencies of Rabbit class, but... Turns out i need to test the collisions, and for that i need to work with RabbitMonoBehaviour in my test. I managed to use reflection to insert my rabbit instance with all dependencies mocked into my monobehaviour, but the problem is - no matter what i do, the Awake executes and tries to create a rabbit with 5 real-code objects! I can overwrite it then with my mocked instance, but the creation code crashes or requires me to create the production versions of all the 5 dependencies of Rabbit class...

Can i somehow skip the execution of Awake in Unity? Or maybe use reflection to override the Awake method for the whole class?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SlowCircuit · Jun 10, 2018 at 03:52 AM 0
Share

You want to skip Awake() entirely on every script? Or are you saying you want to skip Awake() on just one class?

avatar image TheSmokingGnu SlowCircuit · Jun 10, 2018 at 08:14 AM 0
Share

one one class, and only in the test assembly, during test run. But any info on ability to skip awake is welcomed

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TheSmokingGnu · Jun 10, 2018 at 08:16 PM

The best thing i came up with:

Create a mock subclass of the monobehaviour and define an empty Awake() method. Then initialize needed members using reflection.

I am fairly happy with it. Remember, my monobehaviour-to-be-tested was used purely to delegate editor serialized data to my pure C# classes (which were being tested). So this way i eliminated the editor init part altogether, and provided my own mocks through reflection.

The strange reflectionish-way in which unity calls the monobehaviour messages, which made it impossible to mock them in the first place made this workaround usable

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by SlowCircuit · Jun 10, 2018 at 09:54 AM

You may be able to accomplish what you want with pre-processor directives.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/

https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheSmokingGnu · Jun 10, 2018 at 08:07 PM 0
Share

That would be super tedious, and would require to change production code.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can we create playmode tests outside of the main build assembly? 0 Answers

Is there a method on MonoBehaviour that is called on load even if GameObject is disabled? 3 Answers

Awake not called if class has static member 2 Answers

How to unit test a MonoBehaviour which has RequireComponent attribute ? 1 Answer

Unit Testing Rigidbody2D without polluting scene with gameobjects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges