VContainer icon indicating copy to clipboard operation
VContainer copied to clipboard

Unit Test sample

Open PrarR opened this issue 2 years ago • 5 comments

I have a problem with injecting dependencies into Unit Test (Playmode), SceneManager is registered via RootSceneLifetime (Prefab), in the test I use injection via method.

During the test, I get a message that SceneManager is not set, and "Hi MM" does not appear in the console.

Any ideas how to solve this? Or some sample of how to do it correctly?

[Inject]
Private void Construct(SceneManager sceneManager)
{
    Debug.Log("Hi MM");
    _sceneManager = sceneManager;
}

obraz

PrarR avatar Apr 05 '24 08:04 PrarR

Okay, after a lot of testing, I found this solution:

        private void Construct()
        {
            var lifetimeScope = LifetimeScope.Find<RootLifetimeScope>();
            _sceneManager = lifetimeScope.Container.Resolve<SceneManager>();
        }
        [UnityTest]
        public IEnumerator ChangeSliderValue([Values(0f, 0.5f, 1f)] float value)
        {
            Construct();
            
            //test code...
        }

I think it would be useful to add an example of testing to the documentation, and include information about the fact that there are many valuable examples of testing in the VContainer/Assets/Tests folder.

Is it good solution or better try something else? I would appreciate feedback.

PrarR avatar Apr 05 '24 09:04 PrarR

Okay, still one problem

In the Zenject test code:

        private SignalBus _signalBus;
        
        private void CommonInstall()
        {
            PreInstall();
            //Container.Bind<> there
            PostInstall();
            
            _sceneManager = Container.Resolve<SceneManager>();
            _signalBus = Container.Resolve<SignalBus>();
        }

In desired test:

            CommonInstall();
            //test code...       

tests class inherit from ZenjectIntegrationTestFixture

Each time the test was run, the state of the Inject stuff was reset. How do I get the same result for the VContainer (without manually cleanup)?

PrarR avatar Apr 05 '24 09:04 PrarR

Okay, I did some research, I mean something like this:

https://github.com/modesttree/Zenject/blob/master/UnityProject/Assets/Plugins/Zenject/OptionalExtras/TestFramework/ZenjectIntegrationTestFixture.cs

in this class Zenject author is calling:

          ZenjectTestUtil.DestroyEverythingExceptTestRunner(immediate);

https://github.com/modesttree/Zenject/blob/master/UnityProject/Assets/Plugins/Zenject/OptionalExtras/TestFramework/ZenjectTestUtil.cs

It is really helpful to destroy everything VContainer-related in runtime (I want to test features like UI or Scenes) before next test, so I will be grateful for any tips / advices

PrarR avatar Apr 05 '24 16:04 PrarR

If it is really unit testing, then there is no need for a DI container. If everything is written in constructor injection, the test can simply call it.

If for some reason you want to use a DI container in your tests,

  • LifetimeScope.Create() could be used.
  • Alternatively, you can use var builder = new ContainerBuilder() to assemble a container without depending on GameObjects.

hadashiA avatar Apr 17 '24 04:04 hadashiA

If for some reason you want to use a DI container in your tests,

  • LifetimeScope.Create() could be used.
  • Alternatively, you can use var builder = new ContainerBuilder() to assemble a container without depending on GameObjects.

It's supposed for play mode test, especially on player/devices. I noticed the documentation mentioned unit test using the inject method, but I'm unable to make it work.

Image

Using ContainerBuilder not working either, my setup for the play mode test is

// load the scene
var scope = LifetimeScope.Find<GlobalLifetimeScope>();
_container = scope.Container;
_sceneLoader = _container.Resolve<SceneLoader>(); // this works, just find the scope on the loaded scene
var test = new ContainerBuilder().Build().Resolve<SceneLoader>(); // this doesn't

error

Image

It makes sense because the tests were created even before the containers were. Any example to work with unit test during play mode is awesome, for now, I'll just use this "hack" https://github.com/hadashiA/VContainer/issues/657#issuecomment-2039308968

trungnhm1998 avatar Aug 06 '25 11:08 trungnhm1998