Stubs - Lightweight Test Stubs and Detours for .NET

Stubs is a lightweight framework for test stubs and detours in .NET that is enterily based on delegates, type safe, refactorable and source code generated. Stubs was designed support the Code Contracts runtime writter and provide a minimal overhead to the Pex white box analysis. Stubs may be used on any .NET method, including non-virtual/static methods in sealed types.
Features
- Delegate based. Delegates are the building block to specify behaviors of stubs. In .Net, delegates are the natural (and efficient) way of dynamically attaching code to events, so that's what we use.

- Source code generated. No dynamic code, no Reflection Emit, no expression trees, just good old plain C#. Each stubbed method simply invokes a user-provided delegate if provided or execute the default behavior. For example, this snippet is taken from the stub of IFileSystem:

- Type safe. No magic strings, all stubs are type safe and refactorable.
- Lightweight. With the stubs, the overhead of calling a stubbed method is one virtual call. This is pretty much as good as it gets.
- Great debugging support. You can seamlesly step through, in and out of stubbed members.

- Pex friendly. Stubs was specifically designed to support Pex (in fact, it actually ships as part of Pex).
- Code Contracts friendly. When you write interface contracts, the stubs will be instrumented with those contracts by the runtime rewritter.
- Non-sealed Classes and Virtual Methods. not everybody like interfaces, so you can also generate (partial) stubs for abstract classes or even non-sealed classes.
- Moles for Static classes and non-virtual methods. Stubs also provides the ability to detour any .NET method. The test below shows how the DateTime.Now value can be changed using moles.

- Visual Studio Integration. the stubs code generator integrates into the Visual Studio build environment and regenerates seamlessly the stub files.




