tags:

views:

31

answers:

1

I have method (which is part of IMyInteface) like this:

interface IMyInterface
{
  void MyMethod(IList<Foo> list);
}

I have the ClassUnderTest:

class ClassUnderTest  
{  
  IMyInterface Bar {get; set;}

  bool AMethod()  
  {
    var list = new List<Foo>();
    Bar.MyMethod(list);

    return list.Count()>0;
}

My Test with Rhino Mocks looks like this:

var mocks = new MockRepository();  
var myMock = mocks.StrictMock<IMyInterface>();  
var myList = new List<Foo>();
var cUT = new ClassUnderTest();
cUT.Bar = myMock;

myMock.MyMethod(myList);  
//How can I add some items to myList in the mock?   
mocks.Replay(myMock);
var result = cUt.AMethod();

Assert.AreEqual(True, result);

How can I now add some items to myList in the mock?

+1  A: 

Try this:

myMock.Stub(methodInv => methodInv.MyMethod(new List<Foo>()).IgnoreArguments()
    .WhenCalled(invocation => (invocation.Arguments[0] as IList<foo>).Add(new Foo());

So the first lambda function enables the method; the second one specifies what happens in the method.

I haven't tested this yet, so let me know if it's wrong!

Dave Arkell
That was the answer I was looking for! Thanks. Solution works. Great
WaltiD