使用依赖注入的测试方法(结构图)
我有一个工厂方法来创建某个接口的新实例。
我正在使用 StructureMap 创建接口的新实例。
我如何对该方法进行单元测试?
I have got a factory method that create new instances of a certain interface.
I am using StructureMap to create new instances of the interface.
How can I unit test this method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你让工厂将 IContainer 作为 ctor 依赖项,你就可以删除该容器。
如果您配置 Structure Map 来实例化工厂,则 IContainer 应由 Structure Map 自动解析。
编辑:
我正在考虑这样的事情,在测试时将结构图从等式中剔除:
该示例使用Rhino Mocks和NUnit,但当然您可以以任何您想要的方式进行测试和存根。
If you make the factory take an IContainer as a ctor dependency you can stub out the container.
The IContainer should be resolved automatically by Structure Map if you configure Structure Map to instantiate the factory.
Edit:
I was thinking about something like this, taking Structure Map out of the equation when testing:
The example uses Rhino Mocks and NUnit, but of course you can test and stub any way you want.
我终于能够实现我想要的了。
如果您考虑一下,您希望在隔离的环境中执行测试。
因此,我只需要使用模拟对象初始化结构,然后就可以测试我的工厂方法。
I was finally able to achieve what I wanted.
If you will think about it, you want to perform your test in an isolated environment.
So I just needed to initialise structure with a mock object and I was able to test my factory method.