mercredi 31 juillet 2019

Zenject Mono Installer not called in Scene tests under some circumstances

I have a "Main" scene consisting of a softozor game object which is controlled by the player. It's a flappy dinosaur. As a child of that softozor game object, I have setup another game object, Installer, consisting of a Transform component and a PlayerInstaller (Script) component:

image

The PlayerInstaller installs everything necessary for my player's logic. Finally, in the softozor game object, I have added a Game Object Context (Script) where I register the PlayerInstaller:

image

In addition to the softozor game object, I have also defined a SceneContext:

image

You'll notice that all installers lists are empty in that SceneContext. However, without that SceneContext registering nothing, the PlayerInstaller is not triggered. Playing the game with that setup works perfectly well, i.e. the PlayerInstaller is called and I can control my dinosaur to do whatever I want in my game.

So far, so good. Now, consider the following Scene Test:

public class PlayerTests : SceneTestFixture
{
  [Inject]
  private IPlayer _player;

  [UnityTest]
  public IEnumerator TestScene()
  {
    yield return LoadScene("Main");

    _player.Flap();
    yield return new WaitForSeconds(0.01f);
    [...]
  }
}

In that test, the _player member variable is not injected with an object satisfying the IPlayer contract. In fact, the PlayerInstaller.InstallBindings() is not called.

If I, instead, get rid of the Game Object Context (Script) component in my softozor game object, and register the PlayerInstaller in the SceneContext:

image

then I can play the game too, as before, and my test is running, i.e. the PlayerInstaller.InstallBindings() method is called during my Scene Test.

What is wrong with my first attempt where I register the PlayerInstaller in the softozor game object context?

I am working with

  • Zenject ver. 7.3.1
  • Unity 2019.1.8f1 PC, Mac & Linux Standalone

Aucun commentaire:

Enregistrer un commentaire