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
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.