无法为 System.Environment 类创建 Mole
我想在单元测试中替换 Environment.GetFolderPath
的行为,但在为 System
程序集生成 Moles 后,System.Moles
程序集不包含定义类型为MEnvironment
。
I want to replace behavior of Environment.GetFolderPath
in my unittest, but after generation moles for System
assembly, System.Moles
assembly does not contain definition of type MEnvironment
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过在接口类型中存根? sub 可以通过构造函数注入或通过方法输入参数传入。例如,创建接口IEnvironment,包括GetFolderPath方法。如果您已经知道如何执行此操作,请随意继续。否则,下面演示如何在环境类中存根。 (我在手机上输入此内容,所以请耐心等待;)
接下来,创建一个存根:
将存根作为参数传递给您的方法或类构造函数:
为了测试,创建一个模拟类型,或摩尔 MyEnvironment.GetGolderPath。出于性能方面的考虑,创建模拟比模拟类型更可取。模拟只是返回一个值,而不是调用基本方法,就像模型类型一样。无论哪种方式,他的存根都是必要的,所以您也可以进行模拟,因为它可以重用于其他测试。
Have you tried stubbing in an interface type? The sub can be passed in via constructor injection or via method input parameter. For example, create interface IEnvironment, including the GetFolderPath method. If you already know how to do this, then feel free to go on your way. Otherwise, the following demonstrates how to stub in the Environment class. (I am typing this on my phone, so bear with me ;)
Next, create a stub:
Pass the stub in to your method or class constructor as a parameter:
For testing, create a mock type, or mole MyEnvironment.GetGolderPath. Creating a mock is preferable to a moled type, for performance reasons. The mock simply returns a value instead of calling the base method, just as the moled type would. He stub is necessary, either way, so you may as well make the mock, as it can be reused for other tests.