使用Rhino Mock在c#中模拟静态方法
在c#中模拟静态类的最佳方法是什么。例如:使用Rhino Mock在c#中模拟静态方法,c#,.net,rhino-mocks,C#,.net,Rhino Mocks,在c#中模拟静态类的最佳方法是什么。例如: String appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath; public interface IApplicationPhysicalPathProvider { string ApplicationPhysicalPath { get; } } public class AspNetApplicationPhysicalPathProvid
String appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
public interface IApplicationPhysicalPathProvider
{
string ApplicationPhysicalPath { get; }
}
public class AspNetApplicationPhysicalPathProvider : IApplicationPhysicalPathProvider
{
public string ApplicationPhysicalPath
{
get { return HostingEnvironment.ApplicationPhysicalPath; }
}
}
我想模拟这种情况,但需要将其封装在一个带有接口的类中,最好的方法是什么?这种方法适用于所有静态方法吗
这种方法不会产生很多毫无意义的接口和类吗
在c中模拟静态类的最佳方法是什么#
最好的方法是将这些静态调用隐藏在抽象后面。那些是可以伪造的。例如:
String appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
public interface IApplicationPhysicalPathProvider
{
string ApplicationPhysicalPath { get; }
}
public class AspNetApplicationPhysicalPathProvider : IApplicationPhysicalPathProvider
{
public string ApplicationPhysicalPath
{
get { return HostingEnvironment.ApplicationPhysicalPath; }
}
}
此建议适用于任何模拟框架,即使您根本不使用模拟框架
在c中模拟静态类的最佳方法是什么#
最好的方法是将这些静态调用隐藏在抽象后面。那些是可以伪造的。例如:
String appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
public interface IApplicationPhysicalPathProvider
{
string ApplicationPhysicalPath { get; }
}
public class AspNetApplicationPhysicalPathProvider : IApplicationPhysicalPathProvider
{
public string ApplicationPhysicalPath
{
get { return HostingEnvironment.ApplicationPhysicalPath; }
}
}
此建议适用于任何模拟框架,即使您根本不使用模拟框架