Ayende @ Wiki

According to Fowler:

Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ’sent’, or maybe only how many messages it ’sent’.


Relevant Blog post.

You can use mocks as stubs, you can create a dynamic mock and call PropertyBehavior on its methods. The problem is that this is really annoying. Here is the type of code that you have to write for even mildy complex stub:

public interface IAnimal
{
	int Legs { get; set; }
	int Eyes { get; set; }
	string Name { get; set; }
	string Species { get; set; }

event EventHandler Hungry; string GetMood(); }

[Test] public void CreateAnimalStub() { MockRepository mocks = new MockRepository(); IAnimal animal = mocks.DynamicMock<IAnimal>(); Expect.Call(animal.Legs).PropertyBehavior(); Expect.Call(animal.Eyes).PropertyBehavior(); Expect.Call(animal.Name).PropertyBehavior(); Expect.Call(animal.Species).PropertyBehavior(); }


That is far too long and tiring. Fortunately, Rhino Mocks has a builtin support for stubs:
public interface IAnimal
{
	int Legs { get; set; }
	int Eyes { get; set; }
	string Name { get; set; }
	string Species { get; set; }

event EventHandler Hungry; string GetMood(); }


The animal instance that we got is a stub, is will respond correctly to properties and events, and it can be setup so it will return expected results from methods. Note that stubs in Rhino Mocks defaults to ignoring everything, including things that would be invalid in other mock types (such as calling a method that return a value without supplying such value). Furthermore, for the simple scenario, where all you would like to have is a stub, Rhino Mocks provide a static accessor method to make your life easier:

Generate the animal stub using the GenerateStub method.
[Test]
// Tests stub creation with the GenerateStub method
public void CreateAnimalStub_GenerateStub()
{

IAnimal animal = MockRepository.GenerateStub<IAnimal>();

animal.Name = "Snoopy";

Assert.AreEqual( "Snoopy", animal.Name ); }


Generate the animal stub using the MockRepository.Stub method. Use this if you are using .Net 2.0 and do not want to use the GenerateStub method
[Test]
public void CreateAnimalStub_MockRepositoryStub()
{

MockRepository mocks = new MockRepository(); IAnimal animal = mocks.Stub<IAnimal>();

animal.Name = "Snoopy";

Assert.AreEqual( "Snoopy", animal.Name ); }


Use this alternative syntax if you are using .Net 1.1
[Test]
public void CreateAnimalStub_NonGenerics()
{

MockRepository mocks = new MockRepository(); IAnimal animal = (IAnimal) mocks.Stub( typeof ( IAnimal ), null );

animal.Name = "Snoopy";

Assert.AreEqual( "Snoopy", animal.Name ); }


For properties, you do not need to move a stub to replay mode, but if you would like to setup return values for methods, you need to let Rhino Mocks know when you are finished setting up the stub, and then you can move it to replay mode, and let the rest of the test run.

Up: Rhino Mocks Documentation
Next: Rhino Mocks Ordered and Unordered

ScrewTurn Wiki version 2.0 Beta. Some of the icons created by FamFamFam.