Some More RhinoMocks Examples

Here is a little bit of example code testing readonly / writeonly properties on a interface, as well as void methods

var mockRepository = new MockRepository();
var foo = mockRepository.StrictMock<IFoo>();

using (mockRepository.Record())
{
  // Test property is set set to 88;
  foo.TestProperty = 88;

  // Force return value of property to 99
  Expect.Call(foo.TestProperty).Return(99);

  // Force return value of property to 99
  Expect.Call(foo.TestReadOnlyProperty).Return(99);

  // Test property is set to 297
  // could also do Expect.Call(() => foo.TestWriteOnlyProperty = 297);
  foo.TestWriteOnlyProperty = 297;

  // Ensure void method is called (no params)
  // Could also do Expect.Call(() => foo.TestVoidMethodNoParams());
  foo.TestVoidMethodNoParams();
  // Ensure method is called with a specific param.
  // Could also do Expect.Call(() => foo.TestVoidMethod(297));
  // if we wanted to specify stuff on expect.call (e.g. ignore args)
  foo.TestVoidMethod(297);
}

using (mockRepository.Playback())
{
  var bar = new Bar(foo);
  bar.DoSomething();
}

Here is a good RhinoMocks quick reference guide: http://orand.blogspot.com/2007/12/rhino-mocks-quick-reference.html

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>