开发者

How to add items to a list in Rhino Mocks

开发者 https://www.devze.com 2023-01-01 15:14 出处:网络
I have method (which is part of IMyInteface) like this: interface IMyInterface { void MyMethod(IList<Foo> list);

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.Rep开发者_运维技巧lay(myMock);
var result = cUt.AMethod();

Assert.AreEqual(True, result);

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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消