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?
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>();