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; }
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.
Klaus byskov pedersen
source share