Ayende @ Wiki

Rhino Mocks supports mocking classes as well as interfaces. In fact, it can even mock classes that don't have a default constructor! To mock a class, simply pass its type to MockRepository.CreateMock() along with any parameters for the constructor.

To create a stub on a class. This uses the GenerateStub method for creating the mock. Use this ONLY if you do not wish to set expectations on your mock. You only want to use the mock as a STUB
[Test]
public void AbuseArrayList_UsingGenerateStub()
{
    ArrayList list = MockRepository.GenerateStub<ArrayList>();
        
    // Evaluate the values from the mock
    Assert.AreEqual( 0, list.Capacity );
}


Creating a mock on a class using Generics Use this if you are using .Net 2.0
[Test]
public void AbuseArrayList_UsingCreateMockGenerics()
{
    MockRepository mocks = new MockRepository();
    ArrayList list = mocks.CreateMock < ArrayList >();

// Setup the expectation of a call on the mock Expect.Call( list.Capacity ).Return( 999 ); mocks.ReplayAll();

// Evaluate the values from the mock Assert.AreEqual( 999, list.Capacity ); mocks.VerifyAll(); ; }


Use this alternative syntax if you are using .Net 1.1
[Test]
public void AbuseArrayList_UsingCreateMockGenerics()
{
    MockRepository mocks = new MockRepository();
    ArrayList list = (ArrayList) mocks.CreateMock( typeof ( ArrayList ) );

// Setup the expectation of a call on the mock Expect.Call( list.Capacity ).Return( 999 ); mocks.ReplayAll();

// Evaluate the values from the mock Assert.AreEqual( 999, list.Capacity ); mocks.VerifyAll(); ; }


If you want to call the non-default constructor, just add the parameters after the type.

Like this:

// Non-Generics
ArrayList list = (ArrayList)mocks.CreateMock(typeof(ArrayList),500);


// With Generics
ArrayList list = mocks.CreateMock< ArrayList >( 500 );


Up: Rhino Mocks Documentation
Next: Rhino Mocks - Internal Methods

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