I have SceneA (A) and SceneB (B). The B has also a Foo AsSingle and NonLazy installed by the SceneContext Installer. The Foo has an IInitialize implemented to Debog.Log("Hello Errors!"):
yield return LoadScene("SceneA");
yield return new WaitForSeconds(2.0f);
SceneManager.LoadScene("SceneB");
yield return new WaitForSeconds(2.0f);
var newSceneContainer = ProjectContext.Instance.Container.Resolve<SceneContextRegistry>()
.SceneContexts.Single().Container;
Foo foo = newSceneContainer.Resolve<Foo>();
And I guet "Unable to resolve the class", while in the debug I see the hello message of Foo, so the Installer has done its job. Now in case Foo is a MonoBehaviour, on a gameobject with Zeneject binding it as component "from the scene", then without problem it will be resolved.
Please kindly let me know how to resolve, in this case.
**-- Edit -- 1 **
Foo is renamed to "BClass".
A's Installer is empty.
B's Code:
public class BInstaller : MonoInstaller
{
public override void InstallBindings()
{
Container.BindInterfacesAndSelfTo<BClass>().AsSingle().NonLazy();
}
}
public class BClass : IInitializable, IDisposable {
public void Initialize() { Debug.Log("Hello Errors!"); }
public void Dispose() { }
}
Finally the test:
public class ScenesFlow_A_to_B : SceneTestFixture
{
[UnityTest]
public IEnumerator TestScene()
{
yield return LoadScene("SceneA");
//waiting
SceneManager.LoadScene("SceneB");
//waiting
var newSceneContainer = ProjectContext.Instance.Container
.Resolve<SceneContextRegistry>().SceneContexts.Single().Container;
BClass bc = newSceneContainer.Resolve<BClass>() as BClass;
Assert.IsNotNull(bc);// fail!
}
}
-- Edit 2 --
New changes:
public class ScenesFlow_A_to_B : SceneTestFixture
{
[UnityTest]
public IEnumerator TestScene()
{
yield return LoadScene("SceneA");
//SceneManager.LoadScene("SceneB");
yield return SceneManager.LoadSceneAsync("SceneB");
var newSceneContainer = ProjectContext.Instance.Container
.Resolve<SceneContextRegistry>().SceneContexts.Single().Container;
BClass bc = newSceneContainer.Resolve<BClass>() as BClass;
Assert.IsNotNull(bc);
}
}
the result will be this:
TestScene (0.323s)
---
System.InvalidOperationException : Operation is not valid due to the current state of the object
---
at System.Linq.Enumerable.Single[SceneContext] (IEnumerable`1 source) [0x00000] in <filename unknown>:0
at ScenesFlow_A_to_B+<TestScene>c__Iterator0.MoveNext () [0x00089] in ... at UnityEngine.TestTools.TestEnumerator+<Execute>c__Iterator0.MoveNext () [0x00031] in .../TestEnumerator.cs:29
---
Loading scene 'SceneA' for testing
I am class B