0

I am seraching information about to test methods and Moq it's a good alternative, but it can't mock static methods.

However, is a question in this forums, I read that static methods are more efficients (consume less resources) than the non static method. Also, if a method does not use particular data of the object, it's a better option the static method. The question was in this post.

But if the method is static I can't mock the method, so it's seems that in some cases must choose between to be able to test the method or the performance of the static method.

There are some free alternative to moq to test static methods?

Thanks so much.

Community
  • 1
  • 1
Álvaro García
  • 18,114
  • 30
  • 102
  • 193

1 Answers1

1

I do not know of a framework that can mock static methods because I do not believe it's possible except through something like assembly injection (i.e. emulate the assembly, namespace, and class so the method resolves to the injected symbol).

However, I need to ask, do you have code that needs to be so performant that sensitive to the minimal costs from calling an instance method? I highly doubt that's the case, and if it is, you shouldn't be using an environment that can pause your code for many milliseconds to perform a garbage collection. If that is the case, I'd love to know what kind of software you're writing :)

So if your code is not being used in high-frequency trading software, then consider if you'd rather have tested code or extremely marginally more performant code. This reeks to me of premature optimization...

Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88