#29: add ISingleTypeRegistration unit test

pull/32/head
Simon Gockner 6 years ago
parent 47931b3dd1
commit cc52cd3361
  1. 27
      Test.LightweightIocContainer/IocContainerTest.cs
  2. 61
      Test.LightweightIocContainer/SingleTypeRegistrationTest.cs

@ -44,6 +44,11 @@ namespace Test.LightweightIocContainer
void ClearMultitonInstance();
}
private interface IFoo
{
}
private class Test : ITest
{
@ -61,6 +66,11 @@ namespace Test.LightweightIocContainer
{
}
public TestConstructor(IFoo foo, string name)
{
}
}
[UsedImplicitly]
@ -84,6 +94,12 @@ namespace Test.LightweightIocContainer
}
}
[UsedImplicitly]
private class Foo : IFoo
{
}
public class MultitonScope
{
@ -449,6 +465,17 @@ namespace Test.LightweightIocContainer
Assert.AreEqual(callbackTest, test);
}
[Test]
public void TestResolveSingleTypeRegistrationWithFactoryMethod()
{
_iocContainer.Register<IFoo, Foo>();
_iocContainer.Register<ITest>().WithFactoryMethod(c => new TestConstructor(c.Resolve<IFoo>(), "someName"));
ITest test = _iocContainer.Resolve<ITest>();
Assert.NotNull(test);
}
[Test]
public void TestIsTypeRegistered()
{

@ -0,0 +1,61 @@
// Author: Gockner, Simon
// Created: 2019-11-22
// Copyright(c) 2019 SimonG. All Rights Reserved.
using JetBrains.Annotations;
using LightweightIocContainer;
using LightweightIocContainer.Interfaces;
using LightweightIocContainer.Interfaces.Registrations;
using LightweightIocContainer.Registrations;
using Moq;
using NUnit.Framework;
namespace Test.LightweightIocContainer
{
[TestFixture]
// ReSharper disable MemberHidesStaticFromOuterClass
public class SingleTypeRegistrationTest
{
private interface IFoo
{
IBar Bar { get; }
}
private interface IBar
{
}
[UsedImplicitly]
private class Foo : IFoo
{
public Foo(IBar bar)
{
Bar = bar;
}
public IBar Bar { get; }
}
[UsedImplicitly]
private class Bar : IBar
{
}
[Test]
public void TestSingleTypeRegistrationWithFactoryMethod()
{
IBar bar = new Bar();
Mock<IIocContainer> iocContainerMock = new Mock<IIocContainer>();
iocContainerMock.Setup(c => c.Resolve<IBar>()).Returns(bar);
RegistrationFactory registrationFactory = new RegistrationFactory(iocContainerMock.Object);
ISingleTypeRegistration<IFoo> registration = registrationFactory.Register<IFoo>(Lifestyle.Transient).WithFactoryMethod(c => new Foo(c.Resolve<IBar>()));
IFoo foo = registration.FactoryMethod(iocContainerMock.Object);
Assert.AreEqual(bar, foo.Bar);
}
}
}
Loading…
Cancel
Save