StructureMap 在 NUnit 测试之间不会重置
我正在测试一些使用 StructureMap 进行控制反转的代码,当我对同一接口使用不同的具体类时,就会出现问题。
例如:
[Test]
public void Test1()
{
ObjectFactory.Inject<IFoo>(new TestFoo());
...
}
[Test]
public void Test2()
{
ObjectFactory.Initialize(
x => x.ForRequestedType<IFoo>().TheDefaultIsConcreteType<RealFoo>()
);
// ObjectFactory.Inject<IFoo>(new RealFoo()) doesn't work either.
...
}
如果 Test2 使用 RealFoo 自行运行,则可以正常工作。但如果 Test1 首先运行,Test2 最终会使用 TestFoo 而不是 RealFoo。 NUnit 测试不应该是隔离的吗?如何重置 StructureMap?
奇怪的是,如果我不包含初始化表达式,Test2 就会失败。但如果我确实包含它,它就会被忽略......
I'm testing some code that uses StructureMap for Inversion of Control and problems have come up when I use different concrete classes for the same interface.
For example:
[Test]
public void Test1()
{
ObjectFactory.Inject<IFoo>(new TestFoo());
...
}
[Test]
public void Test2()
{
ObjectFactory.Initialize(
x => x.ForRequestedType<IFoo>().TheDefaultIsConcreteType<RealFoo>()
);
// ObjectFactory.Inject<IFoo>(new RealFoo()) doesn't work either.
...
}
Test2 works fine if it runs by itself, using a RealFoo. But if Test1 runs first, Test2 ends up using a TestFoo instead of RealFoo. Aren't NUnit tests supposed to be isolated? How can I reset StructureMap?
Oddly enough, Test2 fails if I don't include the Initialize expression. But if I do include it, it gets ignored...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果必须在测试中使用 ObjectFactory,请在 SetUp 或 TearDown 中调用 ObjectFactory.ResetAll()。
更好的是,尝试将代码从对 ObjectFactory 的依赖中迁移出来。任何需要从容器中取出内容的类(启动方法除外)都可以接受 IContainer,它将自动由 StructureMap 填充(假设类本身是从容器中检索的)。您可以通过 ObjectFactory 的 Container 属性来引用由 ObjectFactory 包装的 IContainer。您还可以完全避免使用 ObjectFactory,而只创建一个您自己管理的 Container 实例(可以使用与 ObjectFactory 相同的方式进行配置)。
If you must use ObjectFactory in your tests, in your SetUp or TearDown, make a call to ObjectFactory.ResetAll().
Even better, try to migrate your code away from depending on ObjectFactory. Any class that needs to pull stuff out of the container (other than the startup method) can take in an IContainer, which will automatically be populated by StructureMap (assuming the class itself is retrieved from the container). You can reference the IContainer wrapped by ObjectFactory through its Container property. You can also avoid using ObjectFactory completely and just create an instance of a Container that you manage yourself (it can be configured in the same way as ObjectFactory).
是的,NUnit 测试应该是隔离的,并且您有责任确保它们是隔离的。解决方案是在测试装置的 TearDown 方法中重置 ObjectFactory。例如,您可以使用 ObjectFactory.EjectAllInstancesOf() 。
Yes, NUnit tests are supposed to be isolated and it is your responsibility to make sure they are isolated. The solution would be to reset ObjectFactory in the TearDown method of your test fixture. You can use ObjectFactory.EjectAllInstancesOf() for example.
当然,它不会在测试之间重置。
ObjectFactory
是InstanceManager
的静态包装器;它通过AppDomain
是静态的,并且当测试在同一个AppDomain
中运行时,这就是它不被重置的原因。您需要TearDown
测试之间的ObjectFactory
或为每个测试配置一个新的Container
(即,避免使用静态ObjectFactory
)。顺便说一句,这是避免全局状态和单例的主要原因:它们对测试不友好。
来自 Google 指南编写可测试代码:
Of course it doesn't reset between tests.
ObjectFactory
is a static wrapper around anInstanceManager
; it is static through anAppDomain
and as tests run in the sameAppDomain
this is why it is not reset. You need toTearDown
theObjectFactory
between tests or configure a newContainer
for each test (i.e., get away from using the staticObjectFactory
).Incidentally, this is the main reason for avoiding global state and singletons: they are not friendly to testing.
From the Google guide to Writing Testable Code: