如何模拟返回带有最小起订量的 int 的方法
我有一个类可以检索一些内容,并且它有一个方法在检索之前需要一些输入(过滤器)。其中一个“输入”调用另一个方法,该方法基本上返回一个 int,我如何使用最小起订量来模拟它?这是一个示例:
namespace MyNamespace
{
public class ConfigMetaDataColumns : MyModel
{
public int FieldID { get { return ValueInt("FieldID"); } }
public int OrderId { get { return ValueInt("OrderId"); } }
public string Label { get { return ValueString("Label"); } }
public string FieldName { get { return ValueString("FieldName"); } }
public int IsReadOnly { get { return ValueInt("IsReadOnly"); } }
}
public class GetDataClass
{
protected OpenSQLAccessLayer m_WITObject;
// Input Properties
public string GroupID;
public string PageName;
// Output Properties
/// <summary>
/// Get Config meta data
/// </summary>
/// <returns></returns>
public IEnumerable<ConfigMetaDataColumns> GetConfigMetaData()
{
var requester = new ListRequester<OpenSQL, ConfigMetaDataColumns>(m_WITObject, "Result[0].RowCount", "Result[0].Row[{0}].");
return requester.Items;
}
public void InitRequest()
{
User user = (User)HttpContext.Current.User;
m_WITObject = user.NewService<OpenSQLAccessLayer>();
m_WITObject.SetInput("MultipleResultSets", 1);
m_WITObject.SetInput("ClientID", Utils.GetClientID());
m_WITObject.SetInput("GroupID", GroupID);
m_WITObject.SetInput("PageName", PageName);
m_WITObject.Retrieve();
}
}
}
这是“GetClientID()”方法:
public static int GetClientID()
{
User oUser = (User)HttpContext.Current.User;
int nClientID;
string sClientID = string.Empty;
if (String.IsNullOrEmpty(oUser.Session("clientid")))
{
Client oClient = new Client();
}
oUser = (User)HttpContext.Current.User;
sClientID = oUser.Session("clientid");
//If we couldn't retrieve it, throw exception
if ( string.IsNullOrEmpty(sClientID) || !int.TryParse(sClientID, out nClientID))
{
throw new Exception("No clientid found in user session, client not authenticated, please login from main page");
}
return nClientID;
}
我只是在寻找一种方法来传递 ClientID 的硬编码值,并使用它来使用 GetDataClass 进行一些单元测试 类。
谢谢。
I have a class that does some retrieving of contents, and it has a method that requires some inputs (filters) before retrieving it. One of the "input" calls another method, which basically returning an int, how do I mock it using MOQ? Here's an example:
namespace MyNamespace
{
public class ConfigMetaDataColumns : MyModel
{
public int FieldID { get { return ValueInt("FieldID"); } }
public int OrderId { get { return ValueInt("OrderId"); } }
public string Label { get { return ValueString("Label"); } }
public string FieldName { get { return ValueString("FieldName"); } }
public int IsReadOnly { get { return ValueInt("IsReadOnly"); } }
}
public class GetDataClass
{
protected OpenSQLAccessLayer m_WITObject;
// Input Properties
public string GroupID;
public string PageName;
// Output Properties
/// <summary>
/// Get Config meta data
/// </summary>
/// <returns></returns>
public IEnumerable<ConfigMetaDataColumns> GetConfigMetaData()
{
var requester = new ListRequester<OpenSQL, ConfigMetaDataColumns>(m_WITObject, "Result[0].RowCount", "Result[0].Row[{0}].");
return requester.Items;
}
public void InitRequest()
{
User user = (User)HttpContext.Current.User;
m_WITObject = user.NewService<OpenSQLAccessLayer>();
m_WITObject.SetInput("MultipleResultSets", 1);
m_WITObject.SetInput("ClientID", Utils.GetClientID());
m_WITObject.SetInput("GroupID", GroupID);
m_WITObject.SetInput("PageName", PageName);
m_WITObject.Retrieve();
}
}
}
This is the "GetClientID()" method:
public static int GetClientID()
{
User oUser = (User)HttpContext.Current.User;
int nClientID;
string sClientID = string.Empty;
if (String.IsNullOrEmpty(oUser.Session("clientid")))
{
Client oClient = new Client();
}
oUser = (User)HttpContext.Current.User;
sClientID = oUser.Session("clientid");
//If we couldn't retrieve it, throw exception
if ( string.IsNullOrEmpty(sClientID) || !int.TryParse(sClientID, out nClientID))
{
throw new Exception("No clientid found in user session, client not authenticated, please login from main page");
}
return nClientID;
}
I'm just looking for a way for me to pass in a hard-coded value for the ClientID, and use this to do some unit testing with the GetDataClass class.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能模拟静态方法。您应该使用一些依赖注入的方法。假设您将
GetClientId
方法作为名为IUtils
的接口的一部分,如下所示:并且您的具体类
Utils
已按上述方式实现,但没有方法是静态的(当然还要实现接口)。现在,您将接口的实现注入到
GetDataClass
中通过更改其构造函数来更改类,如下所示:
在
InitRequest
方法中,将调用Utils.GetClientID()
更改为this.utils.GetClientId()
。您现在已准备好使用模拟实例化您的
GetDataClass
类,如下所示:就是这样。
You cannot mock a static method. You should use some means of dependency injection. Say you make your
GetClientId
method part of an interface calledIUtils
like so:And you have your concrete class
Utils
implemented as above, but without the method being static (and implementing the interface of course).You now inject an implementation of your interface into the
GetDataClass
class by changing its constructor, like so:
In the
InitRequest
method you change the callUtils.GetClientID()
tothis.utils.GetClientId()
.You are now ready to instantiate your
GetDataClass
class with a mock, like so:And that's it.