Skip to content

Commit

Permalink
Added container dispose callback Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonTalmi authored and hadashiA committed Dec 23, 2023
1 parent 8b00eaa commit f57c782
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions VContainer/Assets/Tests/ContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -566,5 +566,24 @@ public void Inject()
var ctorInjectable = new ServiceA(new NoDependencyServiceA());
Assert.DoesNotThrow(() => container.Inject(ctorInjectable));
}

[Test]
public void OnContainerDisposeCallback()
{
NoDependencyServiceA resolvedJustBeforeDispose = null;

var builder = new ContainerBuilder();

builder.Register<NoDependencyServiceA>(Lifetime.Scoped);
builder.RegisterDisposeCallback(resolver => resolvedJustBeforeDispose = resolver.Resolve<NoDependencyServiceA>());

var container = builder.Build();

Assert.That(resolvedJustBeforeDispose, Is.Null);

container.Dispose();

Assert.That(resolvedJustBeforeDispose, Is.Not.Null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static RegistrationBuilder RegisterFactory<TParam1, TParam2, TParam3, TPa
Lifetime lifetime)
=> builder.Register(new FuncRegistrationBuilder(factoryFactory, typeof(Func<TParam1, TParam2, TParam3, TParam4, T>), lifetime));

public static void RegisterDisposeCallback(this IContainerBuilder builder, Action<IDisposable> callback)
public static void RegisterDisposeCallback(this IContainerBuilder builder, Action<IObjectResolver> callback)
{
builder.Register(container => new ActionDisposable(callback, container), Lifetime.Scoped);
}
Expand Down

0 comments on commit f57c782

Please sign in to comment.