New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StaticContext container binding does not properly override bindings in SceneTests #88
Comments
I spent some more time tonight trying to copy the example test into my new project to make sure I wasn't missing something obvious, and I'm still getting the same unexpected result. If I try to override the EnemySpawner.Settings binding in the test to set Again, this is assuming that in the hypothetical example, EnemySpawner.Settings is being bound somewhere else (either with ZenjectBinding, or an installer on SceneContext or ProjectContext), which the example suggests, to me. |
Same problem here. A binding on the Extenject version : 9.1.0 FYI : StaticContext.Bind<Foo>.AsSingle();
yield return LoadScene("MyScene");
StaticContext.Resolve<Foo>(); // instance A
SceneContainer.Resolve<Foo>(); // instance B
ProjectContext.Instance.Container.Resolve<Foo>(); // instance B
// last but not least :
ProjectContext.Instance.Container.ParentContainers.First().Resolve<Foo>(); // instance A !!! |
DetailsSo I've made some investigations, in my tests I do something like Calling P.S: let us pray the debugger :) |
@Haelle Interesting. Thanks for investigating and thanks for the details. I ended up using a crappy workaround for my tests by resolving the current instance of my binding by using Unity's Not ideal, but I guess it'll have to do until there's a better solution or the documentation is updated. |
How can you find you object before calling I ended up resolving my object and modifying it just after the |
You can't. Sorry if it wasn't clear but I'm doing the same thing you mentioned - resolving the object after |
The scene tests documentation has an example where it overrides the enemy spawner via |
I'm running into the same issue where I try to override a binding through the StaticContext before calling LoadScene with a similar example as the one in the docs. So it possible that it isnt working as documented. |
My workaround for this was to add a static callback to my installer which the tests can hook into and override whatever needs to be mocked. public class ComponentInstaller : MonoInstaller<ComponentInstaller>
{
public static Action<DiContainer> LateInstaller;
public override void InstallBindings()
{
// Program's regular bindings go here
Container.BindInterfacesTo....
// Let tests override and rebind
LateInstaller?.Invoke(Container);
}
} Not the prettiest but it works. |
Following up on this topic. The sample SceneTestFixture test, where a binding on StaticContext.Container is used to override a binding in the scene does work. The in the SpaceShip sample scene, the installer binds uses .IfNotBound() to bind the instance. Since the scene test is binding the instance through the StaticContext before the scene is loaded, the binding in the installer is skipped/overridden. |
Describe the bug
Following this example, I should be able to override a binding in SceneContext or ProjectContext using StaticContext in a SceneTestFixture.
However, this doesn't seem to be the case, unless I am misunderstanding the example.
To Reproduce
Steps to reproduce the behavior:
InstallBindings
in the ProjectInstaller class and bind a new, simple class (i.e. a Cat object with a Name property), give the Cat instance the name "Whiskers".yield return LoadScene("SampleScene")
and get the resolved Cat instance withSceneContainer.Resolve<Cat>()
after loading.Cat.Name
).Expected behavior
If I'm understanding the above example correctly, the StaticContext binding should be overriding the ProjectContext binding, and the assertion should pass. The expected name of the resolved Cat instance from the SceneContainer should be "Ajax", but it is actually "Whiskers".
Here is the entirety of my ProjectInstaller class, with the Cat class:
and here is the entirety of my test class:
Extenject and Unity info (please complete the following information):
Additional context
After some debugging, I suspect that the order of binding is going
ProjectContext > StaticContext > ProjectContext
, as if ProjectContext is calling InstallBindings once more at the end, and overriding the overridden binding. I tried moving the StaticContext binding to after the scene loads, as well as trying a bunch of different attachments to the binding call (i.e.AsSingle
,AsTransient
,AsCached
) with no luck at all.The text was updated successfully, but these errors were encountered: