Ruby Style Mocking in .NET

I had a great discussion with a college about the difficulties of understanding the technical parts of the automated Mocking frameworks.

The one point that takes some time to grasp is the fact that  methods are written within the test that are not actually called, but are stubs/mocks that will be called at a later time. This is true to all frameworks. He then suggested that having the ability to swap one type with another would help in understanding the flow of the test. So I whipped together some code to do just that.

Using this code it is possible to swap one type with another. The two types must have the same methods, but the swapping type doesn’t need to have all the methods of the original type.

Here is an example:

public class OriginalClass { public int ReturnOne() { return 1; } public static int StaticReturnOne() { return 1; } }

Static Method Swapping

Suppose we want to fake the static methods of the OriginalClass, we simply write a new class.

public class FakeClass { public static int StaticReturnOne() { return 2; } }

Note: apart from the names of the methods the two classes are not related.

Here is how we fake the calls,

  • We Swap the OriginalClass with the FakeClass by: Swap.StaticCalls()
  • All static calls to OriginalClass methods that exist in the FakeClass are diverted to the FakeClass.
  • We Unswap the types at the end of the test by Swap.Rollback()
[TestMethod] public void SwapStatic_CallsFake() { Swap.StaticCalls<OriginalClass, FakeClass>(); Assert.AreEqual(2, OriginalClass.StaticReturnOne()); Swap.Rollback(); }

Instance Swapping

We can do the same with instances, we can create a new FakeClass and swap the instances.

public class FakeClass { public int ReturnOne() { return 2; } }

Note: again, apart from the names of the methods the two classes are not related, but they both might implement the same interface or base class and thus support refactoring.

Here is how we fake the calls,

  • We Swap the next new OriginalClass with the FakeClass by Swap.NextNew().
  • All calls to that OriginalClass method which exist in the FakeClass are diverted to the FakeClass.
[TestMethod] public void SwapInstance_CallsFake() { Swap.NextNew<OriginalClass, FakeClass>(); OriginalClass swappedInstance = new OriginalClass(); Assert.AreEqual(2, swappedInstance.ReturnOne()); }

Missing Methods

At this stage the missing methods of the swapped type can have 2 behaviors:

  • Call the original class (Default or UndefinedMethods.CallOriginal)
  • Do nothing and Return a default value (UndefinedMethods.Ignore)

This is set in the swap statements.


  1. The business presentation of add on possibly a lot of critical factors while you are extramarital relationships your way through web sites. photo linked add on accept become good enough more effective and may show up with some other volume. list linked commemoration add on needs to ne good, and definately will animate people to buy the solution.
    [url=]nike air max pegasus 89[/url]
    nike air max pegasus 89

  2. favor : Il existe nufactured nombreuses d茅clinaisons du M17x R3, Certaines fonctionnant sous processeur apple fundamental i7 2760QM ou 2670QM (Moins puissants cual l. a,chicago account 茅valu茅e ici), D’autre avec une carte graphique AMD hd 6990M ou une NVIDIA GTX 560M, D’autres encore sont d茅pourvues d’茅crans 120 hertz (3D active). put ceux l脿 l’ensemble des remarques sur l’ergonomie g茅n茅rale restent valab, Mais l’ensemble des shows, l’ensemble des capacit茅s dans l. a,chicago jeux et nufactured qualit茅 l’茅cran seront diff茅rents. Suivant l. a,chicago d茅clinaison choisie, Le prix nufactured vente peut 茅galement varier du plain au double. V茅rifiez donc bien l’ensemble des composants avant promote achat!
    nike sb koston 2 black

Add Comment

Required fields are marked *. Your email address will not be published.