Couple more ctor injection unit tests

This commit is contained in:
Antony Male 2014-02-13 17:43:20 +00:00
parent e71a1f0dc7
commit eb163586cc
2 changed files with 69 additions and 1 deletions

View File

@ -641,7 +641,7 @@ namespace Stylet
}
catch (StyletIoCRegistrationException e)
{
throw new StyletIoCFindConstructorException(String.Format("{0} Required by paramter '{1}' of type {2}.", e.Message, x.Name, this.Type.Name), e);
throw new StyletIoCRegistrationException(String.Format("{0} Required by paramter '{1}' of type {2}.", e.Message, x.Name, this.Type.Name), e);
}
}
return Expression.Constant(x.DefaultValue);

View File

@ -1,4 +1,5 @@
using NUnit.Framework;
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
@ -7,8 +8,75 @@ using System.Threading.Tasks;
namespace StyletUnitTests
{
class C1 { }
class C2
{
public C1 C1;
public C2(C1 c1)
{
this.C1 = c1;
}
}
class C3
{
public C1 C1;
public C2 C2;
public C3(C1 c1, C2 c2)
{
this.C1 = c1;
this.C2 = c2;
}
}
class C4
{
public C1 C1;
public C4([Inject("key1")] C1 c1)
{
this.C1 = c1;
}
}
[TestFixture]
public class StyletIoCConstructorInjectionTests
{
[Test]
public void RecursivelyPopulatesConstructorParams()
{
var ioc = new StyletIoC();
ioc.Bind<C1>().ToSelf();
ioc.Bind<C2>().ToSelf();
ioc.Bind<C3>().ToSelf();
var c3 = ioc.Get<C3>();
Assert.IsInstanceOf<C3>(c3);
Assert.IsInstanceOf<C1>(c3.C1);
Assert.IsInstanceOf<C2>(c3.C2);
Assert.IsInstanceOf<C1>(c3.C2.C1);
}
[Test]
public void UsesConstructorParamKeys()
{
var ioc = new StyletIoC();
ioc.Bind<C1>().ToSelf("key1");
ioc.Bind<C4>().ToSelf();
var c4 = ioc.Get<C4>();
Assert.IsInstanceOf<C1>(c4.C1);
}
[Test]
public void ThrowsIfConstructorParamKeyNotRegistered()
{
var ioc = new StyletIoC();
ioc.Bind<C4>().ToSelf();
ioc.Bind<C1>().ToSelf();
Assert.Throws<StyletIoCRegistrationException>(() => ioc.Get<C4>());
}
}
}