0

I have the function like this :

public class myClass
{
  public static string myFunction()
  {
    return "returnValue";
  } 
}

I want to fake myClass() using FakeItEasy.

And I wrote:

var fakeMyClass = A.Fake<myClass>();
A.CallTo(() => fakeMyClass.myClass()).Returns(timeZoneName);

But it was wrong, said that:

"Cannot access with an instance reference; please use the type name instead."

How to fix this problem or if there is another way to fake static function in class, thank you.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
bobo
  • 1
  • 1
  • 4
    It's impossible. [What can be faked](https://fakeiteasy.github.io/docs/7.4.0/what-can-be-faked/). FakeItEasy is _constrained_ mocking framework. Look at _unconstrained_ tools. I have listed them here: https://stackoverflow.com/a/74406959/5045688 – Alexander Petrov Aug 28 '23 at 06:05
  • Does this answer your question? [Mocking Static Methods](https://stackoverflow.com/questions/5864076/mocking-static-methods) – Renat Aug 28 '23 at 09:15
  • As mentioned above, it's not possible to mock static methods using FakeItEasy, Moq or NSubstitute, because they all require the method to be virtual, and static methods can't be virtual. A commonly used workaround is to use a wrapper with an virtual instance method that calls the static method, and fake the wrapper. – Thomas Levesque Aug 29 '23 at 14:08

0 Answers0