How to mock a method that returns int with MOQ

I have a class that does some content extraction, and it has a method that requires some inputs (filters) before fetching it. One of the input calls another method, which basically returns int, how do I mock it using MOQ? Here is 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 to pass a hard-coded value to a ClientID and use it to perform some unit tests using the GetDataClass class.

Thanks.

+4
c # moq
source share
1 answer

You cannot mock a static method. You should use some addiction injection remedies. Suppose you put your GetClientId method into an interface named IUtils as follows:

 public interface IUtils { int GetClientId(); } 

And you have your specific Utils class implemented as described above, but without a static method (and, of course, an interface implementation). Now you enter the implementation of your interface in the GetDataClass class, changing its constructor like this:

 public class GetDataClass { private readonly IUtils utils; public GetDataClass(IUtils utils) { this.utils = utils; } //SNIP } 

In the InitRequest method InitRequest you change the call to Utils.GetClientID() to this.utils.GetClientId() .

Now you are ready to instantiate the GetDataClass class using the layout, for example:

 var utilsMock = new Mock<IUtils>(); utilsMock.Setup(u => u.GetClientId()).Returns(42); var getDataClass = new GetDataClass(utilsMock.Object); getDataClass.InitRequest(); 

What is it.

+5
source share

All Articles