如何在 Rhino Mocks 中将项目添加到列表中

发布于 2024-09-03 04:19:52 字数 795 浏览 1 评论 0原文

我有这样的方法(它是 IMyInteface 的一部分):

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

我有 ClassUnderTest:

class ClassUnderTest  
{  
  IMyInterface Bar {get; set;}

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

    return list.Count()>0;
}

我使用 Rhino Mocks 的测试看起来像这样:

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);

现在如何将一些项目添加到模拟中的 myList 中?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Spring初心 2024-09-10 04:19:52

试试这个:

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

所以第一个 lambda 函数启用该方法;第二个指定该方法中发生的情况。

我还没有测试过,所以如果有错请告诉我!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文