You'd be better off finding a different example of unit testing with Rhino.Mocks. The example there mocks the actual class under test, which you would never do.
Let's assume that you have a PersonRepository and a PersonService. You want to unit test the PersonService which uses the PersonRepository. Implementation of Person omitted.
public interface IPersonService
{
Person GetPerson( int id );
List<Person> GetPersons();
}
public class PersonRepository
{
public virtual GetPerson( int id )
{
... implementation
}
public virtual GetPersons()
{
... implementation
}
}
public class PersonService : IPersonService
{
private PersonRepository Repository { get; set; }
public PersonService() : this(null) { }
public PersonService( PersonRepository repository )
{
this.Repository = repository ?? new PersonRepository();
}
public Person GetPerson( int id )
{
return this.Repository.GetPerson( id );
}
public List<Person> GetPersons()
{
return this.Repository.GetPersons();
}
}
Now we have unit tests to ensure that the service is properly calling the repository.
public void GetPersonTest()
{
var repository = MockRepository.GenerateMock<PersonRepository>();
var expectedPerson = new Person( 1, "Name" );
repository.Expect( r => r.GetPerson( 1 ) ).Return( expectedPerson );
var service = new PersonService( repository );
var actualPerson = service.GetPerson( 1 );
Assert.AreEqual( expectedPerson.ID, actualPerson.ID );
Assert.AreEqual( expectedPerson.Name, actualPerson.Name );
repository.VerifyAllExpectations();
}
public void GetPersonsTest()
{
var repository = MockRepository.GenerateMock<PersonRepository>();
var expectedPerson = new Person( 1, "Name" );
var listOfPeople = new List<Person> { expectedPerson };
repository.Expect( r => r.GetPersons() ).Return( listOfPeople );
var service = new PersonService( repository );
var actualList = service.GetPersons( );
Assert.AreEqual( 1, actualList.Count );
var actualPerson = actualList.First();
Assert.AreEqual( expectedPerson.ID, actualPerson.ID );
Assert.AreEqual( expectedPerson.Name, actualPerson.Name );
repository.VerifyAllExpectations();
}