0

In my interface

public IMyListInterface : IList<IMyItem> { void Foo(); }

how can I easily create an example for testing classes that use IMyListInterface.

Currently I'm using GenerateStub<MyListInterface>() and delegating the needed methods / properties to a List<IMyItem> list but it's tedious.

Currently to get the following code under test to work

foreach (var match in matchList)

I'm doing the following in my test class

IList<IMyItem> baseList = new List<IMyItem>();
IMyListInterface matchList = MockRepository.GenerateStub<IMyListInterface>();

matchList.Stub(m => m.GetEnumerator()).Return(null).WhenCalled(i => i.ReturnValue = baseList.GetEnumerator());

Is there a better way?

sixeyes
  • 483
  • 3
  • 14
  • Why not just use Rhino.Mocks built-in "Stub" method -- i.e myListStub.Stub(f => f.Foo()) ? Could you show some code of how you're currently using Rhino.Mocks? – PatrickSteele Jan 09 '13 at 22:44
  • The stub for Foo works fine but simulating the list behaviour is tedious as I have to stub Add, RemoveAt, Count etc. I was wondering if there was a way to get Rhino Mocks to allow me to tell it to use a List directly. – sixeyes Jan 10 '13 at 05:51
  • Let's take a step back. Why do you want to mock the list? Usually (not always), mocking is used to stub out an entire dependency. Is there some behavior of the list you want to stub out -- but keep the rest of the functionality the same? More information would help. – PatrickSteele Jan 10 '13 at 14:37
  • I don't want to mock the list. But the item I want to test exhibits list behaviour. Ideally I want to just have a list (e.g new List()) but couldn't see how I can get that. I have a variable called MatchList in my class being tested of type IMyListInterface and I need to get list behaviour from that variable. I tried casting a List to IMyListInterface but that threw an exception. – sixeyes Jan 11 '13 at 13:00
  • Why are you even stubbing the IMyListInterface implementation? If it just has list behavior (add/remove), just use a concrete implementation -- no need to stub it out (as far as I can tell -- you still haven't provided code to show what it is you're trying to test). – PatrickSteele Jan 14 '13 at 12:36
  • OK. I'll try and explain it as best as I can, but so far I've obviously failed. In CSLA lists have complex behaviour compared to an IList. For example the list has an IsValid property that indicates the validity of all items in the list. In the business layer I need this functionality. In the data layer I don't need all this complexity. So I created the interface using IList to allow easier testing in the data layer. However in the business layer I need the IsValid property. Hence my original question. Is it possible to pass on behaviour in Rhino Mocks, a question you've not answered. – sixeyes Jan 14 '13 at 16:50

1 Answers1

0

Implement the interface in an abstract base class for testing:

public abstract MyMockableList : List<MyItem>, IMyListInterface
{
    public abstract void Foo();
}

You can then use MockRepository.GenerateStub<MyMockableList>(), which will function as a normal list (RhinoMocks won't override the methods inherited from List<MyItem>) but you can still stub out the Foo() method.

Alex
  • 7,639
  • 3
  • 45
  • 58