Create a gist now

Instantly share code, notes, and snippets.

@yuta-kaseda /ZenTest Secret
Created Apr 21, 2017

What would you like to do?
ZenTest
using UnityEngine;
using UnityEditor;
using UnityEngine.TestTools;
using NUnit.Framework;
using System.Collections;
using Zenject;
[TestFixture]
public class BindingTest : ZenjectUnitTestFixture
{
public class ReferenceClass
{
public ReferenceClass()
{}
}
public class BindClass
{
public ReferenceClass refe;
public BindClass(ReferenceClass reference)
{
Debug.Log("Newbind");
refe = reference;
}
}
[Test]
public void BindingCalledUnBind()
{
Container.Bind<ReferenceClass>().AsSingle();
Container.Bind<BindClass>().AsSingle();
var res = Container.Resolve<BindClass>();
Assert.True(res.refe != null);
Container.Unbind<BindClass>();
Assert.False(Container.HasBinding<BindClass>());
Container.Bind<BindClass>().AsCached();
res = Container.Resolve<BindClass>();
Assert.True(res.refe != null);
}
[Test]
public void BindingAsCached()
{
Container.Bind<ReferenceClass>().AsSingle();
Container.Bind<BindClass>().AsCached();
var res1 = Container.Resolve<BindClass>();
var res2 = Container.Resolve<BindClass>();
Assert.True(res1.GetHashCode() == res2.GetHashCode());
}
[Test]
public void UnBindingAsCached()
{
Container.Bind<ReferenceClass>().AsSingle();
Container.Bind<BindClass>().AsCached();
var res1 = Container.Resolve<BindClass>();
Container.Unbind<BindClass>();
Container.Bind<BindClass>().AsCached();
var res2 = Container.Resolve<BindClass>();
Assert.False(res1.GetHashCode() == res2.GetHashCode());
}
[Test]
public void BindingAsTransient()
{
Container.Bind<ReferenceClass>().AsSingle();
Container.Bind<BindClass>().AsTransient();
var res1 = Container.Resolve<BindClass>();
var res2 = Container.Resolve<BindClass>();
Assert.False(res1.GetHashCode() == res2.GetHashCode());
}
[Test]
public void UnBindingAsSingle()
{
Container.Bind<ReferenceClass>().AsSingle();
Container.Bind<BindClass>().AsSingle();
var res1 = Container.Resolve<BindClass>();
Container.Unbind<BindClass>();
Container.Bind<BindClass>().AsSingle();
var res2 = Container.Resolve<BindClass>();
Assert.True(res1.GetHashCode() == res2.GetHashCode());
}
[Test]
public void UnReBindingAsSingle()
{
Container.Bind<ReferenceClass>().AsSingle();
Container.Bind<BindClass>().AsSingle();
var res1 = Container.Resolve<BindClass>();
Container.Unbind<BindClass>();
Container.Rebind<BindClass>().AsSingle();
var res2 = Container.Resolve<BindClass>();
Assert.True(res1.GetHashCode() == res2.GetHashCode());
}
[Test]
public void UnBindAsSingleHasBind()
{
Container.Bind<ReferenceClass>().AsSingle();
Container.Bind<BindClass>().AsSingle();
var res1 = Container.Resolve<BindClass>();
Container.Unbind<BindClass>();
Assert.False(Container.HasBinding<BindClass>());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment