协助伪造框架存根 [英] Assistance on Fakes Framework Stubs
问题描述
大家好,
我正在尝试使用Fakes Framework对基于Entity Framework的数据访问层进行单元测试.
I am trying to use Fakes Framework to unit test my Entity Framework based data access layer.
我创建的是伪数据库上下文,而不是真实的数据库上下文.我正在使用依赖注入来传递伪数据库上下文.
I have created fake database context instead of real database context .I am passing the fake database context using dependency Injection.
伪数据库上下文(和真实数据库上下文)类扩展了一个接口(IContext),在该接口中Iam需要为两种方法定义主体.而且我真的不知道如何处理这两种方法,但是我需要针对不同的场景使用不同的返回值. 为什么我不能对固定的返回值进行硬编码.
The fake database context (and the real database context) class extends an interface(IContext) where Iam required to define the body for two methods. And i really dont know how to handle these 2 methods.But I need different return values for different scenarios.Thats why I can't hard code the fixed return value.
因此,我决定假冒我对返回值有更多控制权的那两种方法的行为.
Hence I decided to fake the behavior of those 2 methods where i have more control on return values.
如果我为接口(IContext)创建存根,该接口对这2种方法都具有模拟实现,则它将创建一个指向StubIContext的IContext类型的新实例.班级(我不想要)
If I create stub to the interface (IContext) which has mock implementation for those 2 methods it creates a new instance of type IContext pointing to StubIContext class(Which I dont want)
因此,我必须使用新创建的存根实例而不是实际的虚假数据库上下文实例进行依赖注入.
So I have to use the newly created stub instance instead of the actual fake database context instance for my dependency injection.
那么有没有一种方法可以仅在真正的伪上下文实例上为这两种方法定义伪行为?(一种扩展了IContext)
So Is there a way where I can define fake behavior for those two methods only on the real fake context instance?(one extending the IContext)
我希望我能正确传达问题...
I hope i conveyed issue properly...
感谢&问候
Samba
Thanks & Regards
Samba
推荐答案
桑巴舞,
我正在尝试让熟悉此主题的人参与进来这个问题.可能会有一些时间延迟.感谢您的耐心.
最好的问候
这篇关于协助伪造框架存根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!