There’s a very useful article by Stephen Walther here. Some parts do need to be updated, though:
- Rhino Mocks downloads have moved to hibernatingrhinos.com
- The warning about using assemblies downloaded from the internet in Windows Vista also applies to Windows 7:
One warning about using Rhino Mocks on Windows Vista. Vista will prevent you from using assemblies that you download from the Internet. Before you reference the Rhino.Mocks.dll assembly in Visual Studio, you need to Unblock the assembly: right-click the assembly file and click the Unblock button under the General tab (see Figure 1).
If you reference the assembly before doing this, you’ll need to remove the reference, delete the rhinomocks.dll from the bin folder, and then re-add the reference to the enabled assembly file before you’ll be able to use it.
- To stub an object:
var myStubbedObject = MockRepository.GenerateStub<IStubbedObject>();
- To stub a method:
myStubbedObject.Stub(x => x.MyStubbedMethod(myParameter)).Return("testValue");
- To stub a property:
myStubbedObject.Stub(x => x.MyStubbedProperty).Return("testValue");
- If you get an exception message along the lines of ‘Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method)’ then make sure you have stubbed the interface and not the implementing class (assuming that you are working with classes which implement an interface, of course).
- You can’t stub static methods with Rhino Mocks. If you want to stub
Filemethods, for example, you can get round this issue by creating a wrapper class and stubbing that instead.
- Remember that you don’t have to stub lists if what you need is a list of stubbed objects – just use a real list and add the stubbed objects to it!