[英]Mocking static methods in c# using Rhino Mock
What is the best approach for mocking out a static class in c#. 在c#中模拟出静态类的最佳方法是什么? For instance:
例如:
String appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
I want to mock this but will need to wrap it in a class with an interface, what is the best approach and does this approach work for all static methods? 我想对此进行模拟,但是需要将其包装在带有接口的类中,什么是最好的方法?这种方法是否适用于所有静态方法?
Will this approach not make a lot of allmost pointless interfaces and classes? 这种方法会不会构成很多毫无意义的接口和类?
What is the best approach for mocking out a static class in c#
在C#中模拟静态类的最佳方法是什么?
The best approach is to hide those static calls behind abstractions. 最好的方法是将这些静态调用隐藏在抽象后面。 Those can be faked.
那些可以伪造。 For instance:
例如:
public interface IApplicationPhysicalPathProvider
{
string ApplicationPhysicalPath { get; }
}
public class AspNetApplicationPhysicalPathProvider : IApplicationPhysicalPathProvider
{
public string ApplicationPhysicalPath
{
get { return HostingEnvironment.ApplicationPhysicalPath; }
}
}
This advice holds for any mocking framework and even if you don't use a mocking framework at all. 该建议适用于任何模拟框架,即使您根本不使用模拟框架也是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.