1

How to switch scene during scene test, in a class extending SceneTestFixture?

I need to make a test case while extending SceneTestFixture , where during the test I load Scene 'A' and it set a condition to ProjectContext and then in the same test switch to Scene 'B' and have it to test if the condition is set by 'A', if not to return back to 'A'.

If I try to LoadScene() following the Zenject documentation, then I can't reuse it to switch to next scene. Loading additive is not what I need, so not to use LoadScenes(), and if I use LoadScene() to load 'A', but then by standard Unity SceneManager switch to another scene, then the SceneContainer is Not updated to the new scene and I don't know how to update it. This is an example how my code fails and the flow:

yield return LoadScene("SceneA");
AClass aclass = Container.Resolve...
Assert.IsNotNull(aclass);// and I can access this aclass, but then:
aclass.OnChange_SceneB();// in scene 'A' switch by standard Unity SceneManager
yield return null;
BClass bclass = Container.Resolve...// I expected that Container will auto-update, but...
// error: resolve failed!

I got to source of SceneTestFixture here, but I couldn't figure out, how to reload the Container to the new SceneContext?

Ariam1
  • 1,673
  • 2
  • 13
  • 32

1 Answers1

2

This is an issue with Zenject's SceneTestFixture class. It assumes that the scene will not change at runtime currently. I added an issue for it here. In the meantime, you can get the scene context for the current scene by doing something like this:

yield return LoadScene("FirstScene");

yield return new WaitForSeconds(2.0f);

var newSceneContainer = ProjectContext.Instance.Container.Resolve<SceneContextRegistry>()
    .SceneContexts.Single().Container;

newSceneContainer.Resolve<Foo>();
Steve Vermeulen
  • 1,406
  • 1
  • 19
  • 25
  • Could you please let me know how with the above technique I could possibly do LoadScene("SecondScene") and update the container to it? Thank you for the support, the Zenject and the Modest Tree. – Ariam1 Nov 04 '18 at 19:55
  • Also, I tried the code, but ...SceneContexts appear to not to give to Single(), unfortunately. – Ariam1 Nov 04 '18 at 22:19
  • You have to add `using System.Linq` to use Single(). I think what I'll do is I'll just do a proper fix and allow calling `LoadScene` twice in the same test then let you know – Steve Vermeulen Nov 04 '18 at 23:37