Thursday, January 14, 2010

Rhino Mocks : How to mock read-only properties.

As part of my never-ending quest to do something big, I started working on an application for which I am learning to use Rhino Mocks and I thought it would make a small and easy example to understand mocks. I have not particularly followed TDD approach so far, but anyway here is my attempt to show how you would use Rhino Mocks to stub/mock (am yet to understand when to use what…I mean i know the difference … just did not develop enough maturity in that aspect).

Let us say I have some interface which is as simple as shown below.

public interface IColumn {
public string Name { get; }
public string DataType { get; }

Now if I were to mock/stub this interface, I had some problems with Rhino Mocks throwing exceptions on how the property should have a getter/setter when I was trying to configure the mock such that whenever Name property is accessed you should return "Krishna Bhargava" and for DataType you should return "System.String". I tried different mechanims like using Expect.On(col).Call(it.Name).Return("Krishna Bhargava") or tried to define property behavior (PropertyBehavior()). Finally after struggling for an hour, looking at various examples online from good people like you and me, I was able to come up with proper code that can generate a stub for the column defined in this interface. The code snippet shows how you can use Rhino Mocks (which i think the most convenient framework - i tried Moq (somehow it does not click with me), NUnit.Mocks( too much ground work)) so that u can generate stubs for read-only properties! Unfortunately at the moment, I cannot comment much on the Rhino Mocks classes .. I hope the code snippet is easy to understand.

private IColumn MockColumn(string name, string type)
IColumn col = MockRepository.GenerateStub();
col.Stub(it => it.DataType).Return(Type.GetType(type));
col.Stub(it => it.Name).Return(name);
return col;

you can later use this method as shown below

public void ColumnStubNameCanBeSet()
IColumn col = MockColumn("name", "String.String");
Assert.AreEqual(typeof(string), col.DataType);

/*a little bit more real world usage of a column stub is shown below. */
public void ValuesCanBeSetOnARecord()
IRecord record = new Record(); //NOT DESCRIBED in this blog, but this is my personal class....
record.SetValue(MockColumn("name", "System.String"), "Krishna Vangapandu");
record.SetValue(MockColumn("age", "System.Int32"), 25);
Assert.AreEqual("Krishna Vangapandu",record.Value("name"));
Assert.AreEqual(25, record.Value("age"));

And by the way, Castle Windsor has the best and easiest configuration schema in all the IoC containers I played with. I personally like Unity Container but its configuration is a mess and over-engineered. I will try to post an implementation of Observer-Pattern where observers and observables are linked with pure configuration - the best use I made of IoC container so far!

1 comment:

Grant_C said...

Thanks, useful example.