I’ve been trying to write a unit test for my ITriggerEventsJob but the two methods I have tried so far have failed.
The first method I tried was by creating the Unity Physics-systems in my test world and trying to get them to recognize an actual collision between two entities with Collision-components. I failed to get this working, seemingly because there are a lot of physics-systems and I’m not sure I created them all correctly.
I did also try not manually creating systems at all, but instead used the
DefaultWorldInitialization.Initialize-method to create the world with all systems, but this did not result in any registered triggers either. I’m thinking this might be because of incorrectly applied components to my test entities, as when I try the collision in the actual editor (using game object to entity conversion) the trigger collision is handled in my job.
The second method I tried was by manually calling the Execute-method of my ITriggerEventsJob, but since it requires a TriggerEvent-parameter I had to create one and pass one in. The problem here is that while I can create a TriggerEvent-instance, I can not set any of its values since they are all based on an internal field of type TriggerEventData. Good thing is that TriggerEventData contains a method for creating a TriggerEvent from it. Bad thing is that the TriggerEventData-struct is private and the CreateTriggerEvent-method is internal. (Mocking the TriggerEvent using NSubstitute is not an option either since it can’t mock structs. If anyone knows about a mocking-library that allows this then that would help!)
My last-ditch efforts in trying to manually call the Execute-method lead me down the route of trying to use reflection to modify the internal fields of TriggerEvent to allow me to set the EntityPair to my two test-entities that I want to trigger the job. This also failed, most probably because I have zero experience using reflection and so I had trouble actually setting the values of the internal fields as they are structs and can not be modified since they are value-types (I believe).
In summary, what I need is some way of artificially creating a TriggerEvent for my two entities that I want to collide so that my ITriggerEventsJob runs. Anyone got any ideas?