我们目前有一套通过 MbUnit 测试套件运行的集成测试。我们正在重构大部分代码以使用 IOC 框架 (StructureMap)。
我想在 MBUnit 测试运行程序启动时配置/初始化容器一次,使用我们在生产中使用的相同注册表代码。
有没有办法在 MbUnit 中实现这一点?
(编辑)MbUnit 的版本是 2.4.197。
我们目前有一套通过 MbUnit 测试套件运行的集成测试。我们正在重构大部分代码以使用 IOC 框架 (StructureMap)。
我想在 MBUnit 测试运行程序启动时配置/初始化容器一次,使用我们在生产中使用的相同注册表代码。
有没有办法在 MbUnit 中实现这一点?
(编辑)MbUnit 的版本是 2.4.197。
找到了。AssemblyCleanup 属性。
我了解您只想为整个测试运行启动一个容器,并将其作为跨测试套件执行使用的容器。MBUnit文档使您看起来可以使用 TestSuiteFixture 和 TestSuiteFixtureSetup 来完成您想要的工作。
我想从 StructureMap 用户和测试驱动开发人员的角度来谈谈。
我们很少在我们的测试套件中使用容器,除非我们明确地测试从容器中拉出东西。必要时,我使用下面的抽象测试基类(警告我们使用 NUnit):
[TestFixture] public abstract class with_container { protected IContainer Container;
[TestFixtureSetUp]
public void beforeAll()
{
Container = new ServiceBootstraper().GetContainer();
Container.AssertConfigurationIsValid();
}
} public class Bootstraper { public Bootstraper() { ObjectFactory.Initialize(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));
}
}
抱歉,如果这不是您想要的。正是这些技术对我们非常有效,我想分享。