Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
NUnit Action Container magic
public interface IConfigurator
{
T GetInstance<T>();
void Inject<T>(T instance);
}
public interface INeedToConfigure
{
IConfigurator Configurator { set; }
}
[TestFixture]
[UseContainer]
public class SampleTest : INeedToConfigure
{
public IConfigurator Configurator { set; private get; }
[Test]
public void SimpleTestOne()
{
}
[Test]
public void SimpleTestTwo()
{
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface,
AllowMultiple = true)]
public sealed class UseContainerAttribute: Attribute, ITestAction, IConfigurator
{
private IContainer container;
public void BeforeTest(TestDetails details)
{
this.container = Root.Container.GetNestedContainer();
INeedToConfigure configure = details.Fixture as INeedToConfigure;
if(configure != null) {
configure.Configurator = this;
}
}
public void AfterTest(TestDetails details)
{
this.container.Dispose();
}
public ActionTargets Targets
{
get { return ActionTargets.Suite; }
}
public void Inject<T>(T instance)
{
this.container.Configure(cfg => cfg.For<T>().Use(instance));
}
public T GetInstance<T>()
{
return this.container.GetInstance<T>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.