Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3304572
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:04:35+00:00 2026-05-17T21:04:35+00:00

I have a class that does some retrieving of contents, and it has a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:04:35+00:00Added an answer on May 17, 2026 at 9:04 pm

    You cannot mock a static method. You should use some means of dependency injection. Say you make your GetClientId method part of an interface called IUtils like so:

    public interface IUtils
    {
        int GetClientId();
    } 
    

    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:

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

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

    You are now ready to instantiate your GetDataClass class with a mock, like so:

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

    And that’s it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has about 20-some methods in it. Each one does
So assume I have a class that has an init method that does something
I have a static util class that does some string manipulation on a bit
I have a Class that retrieves some data and images does some stuff to
I have a class that does some generic code for Writing to a Database.
The problem is this: I have an abstract class that does some work in
let's say I have a class that does some calculations. This set of calculations
I have a constructor in a class that does some kind of logic: public
I have a class that does some time-consuming calculations. I'm trying to performance test
We have a class library that does some basic operations similar to an ORM,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.