有没有办法在 FakeItEasy 中从 System.Type 对象创建一个假的?
有没有办法在 FakeItEasy 中从 System.Type 对象创建一个假的?类似于:
var instance = A.Fake(type);
我尝试为 AutoFac 编写一个假容器,它会自动返回所有已解析类型的假值。我查看了 FakeItEasy 的代码,所有支持此功能的方法都位于内部类后面,但我发现接口 IFakeObjectContainer 看起来非常有趣,但实现仍然需要对象注册,这正是我想要解决的问题。
Is there any way to create a fake from a System.Type object in FakeItEasy? Similar to:
var instance = A.Fake(type);
I try to write a fake container for AutoFac that automatically return fakes for all resolved types. I have looked in the code for FakeItEasy and all methods that support this is behind internal classes but I have found the interface IFakeObjectContainer that looks pretty interesting, but the implementations still need registration of objects that is the thing that I want to come around.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 FakeItEasy 2.1.0 开始(但请考虑升级到最新版本以获得更多功能和更好的错误修复),您可以 从类型创建一个假的,如下所示:
如果您必须使用早期版本,则可以使用一些基于反射的方法为 A.Fake() 方法创建方法信息。 (因为它是关于自动模拟,所以这实际上不应该成为问题)。
As of FakeItEasy 2.1.0 (but do consider upgrading to the latest release for more features and better bugfixes), you can create a fake from a Type like so:
If you must use an earlier release, you could use some reflection based approach to create a method info for the A.Fake() method. (since it's about auto mocking this shouldn't be a problem really).
最好使用注册处理程序来完成此操作。您应该研究 AutofacContrib.Moq 如何实现其
MoqRegistrationHandler
。您会看到它实际上是使用通用方法MockRepository.Create
来创建假实例。为 FakeItEasy 创建类似的处理程序应该非常简单。This is best done using a registration handler. You should look into how AutofacContrib.Moq implements its
MoqRegistrationHandler
. You'll see that it is actually using the generic methodMockRepository.Create
to make fake instances. Creating a similar handler for FakeItEasy should be quite simple.