tags:

views:

118

answers:

1

If I have the following class, how do I use RhinoMocks to mock the Count off of Things, ie myStuff.Things.Count?:

  public class myStuff : ImyStuff
    {
               private Dictionary<String,String> _things;

...Other Code

         public Dictionary<string, string> Things
         {
          get
          {
           return _things;
          }
         }

 ...Other Code  

    }
+1  A: 

As you have written the code in your question, that is not possible.

It is difficult to see whether the Things property is a member of the ImyStuff interface, but even if it is, the return type is a Dictionary<string, string>, and the Count property of Dictionary<string, string> is not virtual.

You can only mock virtual (including abstract) members. That is true for all dynamic mock frameworks (with the exception of TypeMock).

What you could do is to change the signature of the Things property to return IDictionary<string, string>. That would enable you to mock the Count property.

It's been a while since I used Rhino Mocks (I use Moq these days), but it should go something like this:

var thingsStub = MockRepository.GenerateStub<IDictionary<string, string>>();
thingsStub.Stub(t => t.Count).Return(3);

var myStuffStub = MockRepository.GenerateStub<ImyStuff>();
myStuffStub.Stub(s => s.Things).Return(thingsStub);
Mark Seemann