Stubs is a framework for generating stubs (that we announced earlier with Pex). The framework generates stubs (not mocks) that are
- lightweight, i.e. relying on delegates only,
- strongly typed, i.e. no magic strings – refactoring friendly,
- source code generated, i.e. no dynamic code or expression trees,
- great debugging experience, i.e. break and step through stubs,
- minimalistic API (there is almost none),
- friendly for Pex!
More details about Stubs are also available in the stubs reference document.
Quick Start
Let’s start from a simple interface IFileSystem for which we want to write stubs:
public interface IFileSystem
{
void WriteAllText(string fileName, string content);
string ReadAllText(string fileName);
}
Stubs relies solely on delegates to attach behaviors, and leverages the lambda syntax from C# 3.0 to accomplish pretty much anything:
// SFileSystem was generated by Stubs and stubs IFileSystem
var stub = new SFileSystem();
// always returns “...”
stub.ReadAllText = (me, file) => "...";
// expectations: checks file = “foo.txt”
stub.ReadAllText = (me, file) =>
{
Assert.AreEqual("foo.txt", file);
return "...";
};
// storing side effects in closures: written saves content
string written = null;
stub.WriteAllText = (me, file, content) => written = content;
// hey, we can do whatever we want!
stub.ReadAllText = (me, file) =>
{
if (file == null) throw new ArgumentNullException();
return "...";
};
// downcast to the interface to use it
IFileSystem fs = stubs;
Anything is possible… as long as C# (or your favorite language) allows it.
Anatomy of a Stub
Each stubbed method has an associated field delegate that can be set freely (e.g. WriteAllText and ReadAllText). If this delegate field is set, it will used when the method is called; otherwize a default action occurs. Let’s see this with a simplified stub of the IFileSystem interface, SFileSystem, which shows how the IFileSystem.WriteAllText is implemented:
class SFileSystem
: StubBase<SFileSystem>
, IFileSystem
{
public Action<SFileSystem, string, string> WriteAllText; // attach here
void IFileSystem.WriteAllText(string fileName, string content)
{
var stub = this.WriteAllText;
if (stub != null)
stub(this, fileName, content); // your code executed here
else
this.DefaultStub.VoidResult(this);
}
}
The actual generated code may look more complicated because it contains custom attributes for debugging, comments and globally qualified types to avoid name clashing:
Code generation: How does it work?
Stubs is a single file generator that pre-generates stubs to source code. Stubs also monitors the build and regenerates the source code when a change occurs.

The stub generation is configured through an XML file (.stubx file) that contains which code and how it should be generated. The generated code is saved in a ‘Designer’ file, similarly to other code generation tools (type dataset etc…).
Great debugging experience
A cool side effect of simply using delegates: you can step through and debug your stubs! This is usually not the case with mock framework using dynamic code. Below, you can see that one can set breakpoints in the body of a stub and debug as usual.
Where do I get it?
The Stubs framework comes with Pex but you can use it in any unit testing activity. It provides a simple lightweight alternative to define stub for testing. Pex can be downloaded from http://research.microsoft.com/pex .