0

EDIT: looking for a C# mocking framework that allows mocking static methods This question doesnot provide solution for my problem.

I am not able to do mock unit testing for static methods at free of cost (Please suggest if there are any).

Is there any way that i can customize mock testing without any external dll usage.

Please suggest a solution to get me started in my customized mock unit testing.

Even tutorial links would be of great help.

Community
  • 1
  • 1
Ebenezar John Paul
  • 1,570
  • 5
  • 20
  • 41
  • 3
    Your question is unclear. What is it you want to mock? Why can't you use an existing mocking framework? – David Arno Nov 04 '13 at 12:22
  • @DavidArno: Using existing framework(moq) i am able to mock non-static methods. It seems that mocking static-methods are not free of cost. So i would like to know s there any way that i can customize the mock testing myself to test static methods OR is there any free resource to mock static methods. – Ebenezar John Paul Nov 04 '13 at 12:36
  • Try MS Moles. It is replaced with new MS Fakes , but still you might be able to use it. Have a look at the documentation. http://research.microsoft.com/apps/mobile/showpage.aspx?page=/en-us/projects/moles/ if that does not work, I would think about making the API testable such creating testable wrappers etc. – Spock Nov 04 '13 at 12:45

3 Answers3

4

There are existing frameworks that allow the mocking of static methods: looking for a C# mocking framework that allows mocking static methods

Having said that, if it is your own code that you want to mock, follow this simple rule of thumb: don't write static methods that have side effects. That way, your static methods won't need mocking and methods with side-effects (that often do want mocking) can be easily mocked with an existing framework.

If you must mock statics and you don't want to pay for an existing framework, then you'll need to write your own. One route would be to use something like PostSharp (http://www.postsharp.net), which supports modifying static behaviour of methods, including method interception. Have a read of http://www.postsharp.net/aspects/method-decorator for more information. There is a free version of PostSharp available, but you'll have to check the license details to see if they'd apply in your case.

Community
  • 1
  • 1
David Arno
  • 42,717
  • 16
  • 86
  • 131
1

Not sure why you would not like to use existing mock frameworks (they are plenty of free libs like Rhino mocks )

However, you may implement your own mocks using Reflection.Emit.

Here is a good tutorial about how to emit a dynamic type at runtime (you could dynamically implement an interface which systematically calls an event to get your methods result for instance).

This is a way to create interception proxies (which may be used to mock interfaces, but the field of application of Reflection.Emit is much wider)

And this is part of the .Net framework. No thirdparty library required.

[Edit] The only way to override static method content is using IL injection. This is a quite nasty practice, but efficient (only use it for tests though... that's hacking the CLR!).

See this link to know more about it. This allows to replace a method's IL by another's (or patch it manually).

Olivier
  • 5,578
  • 2
  • 31
  • 46
0

If you want to mock static methods without using a framework you can consider a technique where you abstract the static method through a virtual method in the class under test.

The idea is that you can override the virtual method via a stub or the test class itself.

Look at the following example: http://www.unit-testing.net/CurrentArticle/How-To-Remove-Data-Dependencies-In-Unit-Tests.html

TGH
  • 38,769
  • 12
  • 102
  • 135