Stubs - Getting Started with Stubs
Stubs is a stubbing framework solely based on delegates.
Basic use of stubs
Stubs are always located in a subnamed namespace '.Stubs' and their names is created by prepending 'S' to the type name. Stubs implement explicitely the interface members, thus simply cast them to the stubbed interface to start using them in the test.
// create stub and attach behavior
var stub = new SIFoo(); // stuf of IFoo
stub.Bla = () => Console.WriteLine("hello");
// cast the stub to the interface under test
IFoo foo = stub;
foo.Bla(); // this will call our lambda
Methods
// method with parameters use a delegate with the same signature
stub.Add = (a, b) => a + b;
// one last out parameters is supported
stub.TryParse = delegate(string value, out int result) {
return int.TryParse(value, out result);
}; Properties
// getters and setters have their own delegates
stub.NameGet = () => "bill";
stub.NameSet = (value) => ; // do nothing
Recursive Properties
// Stubs provides helper method to enable recursive stubs
// ChildGetAsStub instantiate SIChild as needed
// and stores it in the stub.
stub.ChildGetAsStub<SIChild>().NameGet = () => "joe";
Events
// event backing fields are exposed publicly
// simply invoke it!
stub.NameChanged();
Partial Stubs
// call base instead of the fallback behavior
var stub = new SFooBase() { CallBase = true };Stubs with state
// locals can be used to hold state. C# will emit the closure for you.
string name = null;
// the C# compiler will create a closure to hold namestub.NameGet = () => name;
stub.NameSet = (value) => name = value;
Generating Stubs
The code generation of Stubs is configured through .stubx files (see Add New Item -> Stubs).
<Stubs xmlns="http://schemas.microsoft.com/stubs/2008/">
<Assembly Name="FooBar" />
</Stubs>
.stubx files must be located on the root any C# project and the Stubs code generator will automatically emit the stubs.



