我了解到,您希望在整个测试运行中只启动一个容器,并将其作为跨测试套件执行使用的容器。这
MBUnit docs
让它看起来像你可以使用TestSuiteFixture和TestSuiteFixtureSetup来完成你想要的事情。
我想从StructureMap用户和测试驱动开发人员的角度发言。
我们很少在测试套件中使用容器,除非我们明确地测试从容器中提取东西。必要时,我使用下面的抽象测试基类(警告我们使用NUnit):
带容器的公共抽象类
受保护的IContainer容器;
[TestFixtureSetUp]
public void beforeAll()
{
Container = new ServiceBootstraper().GetContainer();
Container.AssertConfigurationIsValid();
}
公共类Bootstraper
{
公共启动程序()
{
对象工厂。初始化(x=>
{
//在这里登记东西
});
}
public IContainer GetContainer()
{
return ObjectFactory.Container;
}}
对于常规测试,我建议您跳过常规容器,只使用StructureMap附带的自动模拟容器。这是我们使用的另一个方便的抽象测试基类。
public abstract class Context<T> where T : class
{
[SetUp]
public void Setup()
{
_services = new RhinoAutoMocker<T>(MockMode.AAA);
OverrideMocks();
_cut = _services.ClassUnderTest;
Given();
}
public RhinoAutoMocker<T> _services { get; private set; }
public T _cut { get; private set; }
public SERVICE MockFor<SERVICE>() where SERVICE : class
{
return _services.Get<SERVICE>();
}
public SERVICE Override<SERVICE>(SERVICE with) where SERVICE : class
{
_services.Inject(with);
return with;
}
public virtual void Given()
{
}
public virtual void OverrideMocks()
{
}
}
以下是使用此上下文测试器的基本测试:
[TestFixture]
public class communication_publisher : Context<CommunicationPublisher>
{
[Test]
public void should_send_published_message_to_endpoint_retrieved_from_the_factory()
{
var message = ObjectMother.ValidOutgoingCommunicationMessage();
_cut.Publish(message);
MockFor<IEndpoint>().AssertWasCalled(a => a.Send(message));
}
}
很抱歉,如果这不是你想要的。这些技术对我们来说非常有效,我想分享一下。