简体   繁体   English

如何将期望中的参数传递给Rhino Mocks中的返回值?

[英]How to pass a parameter in an expectation to the return value in Rhino Mocks?

I want to mock this interface: 我想模仿这个界面:

interface IA {
  IB DoSomething(IC arg)
}

in a way that simulates an implementation like this: 以某种方式模拟这样的实现:

class A : IA {
    public IB DoSomething(IC arg) { return new B(arg); }
}

How can I do that? 我怎样才能做到这一点? From other similar questions, it's supposed to be something like this: 从其他类似的问题来看,它应该是这样的:

MockRepository.GenerateMock<IA>().Expect(x => x.DoSomething(null)).IgnoreArguments().Callback<IC>(arg => new B(arg))

But i can't get it to work. 但我无法让它发挥作用。 I'm using RhinoMocks 3.6 我正在使用RhinoMocks 3.6

var mock = MockRepository.GenerateMock<IA>();
mock
  .Stub(x => x.DoSomething(Arg<IC>.Is.Anything)
  // return a new instance of B each time
  .WhenCalled(call => call.ReturnValue = new B((IC)call.Arguments[0]))
  // make rhino mock validation happy
  .Return(null);

Here is a typesafe example: 这是一个类型安全的例子:

var mockA = MockRepository.GenerateMock<IA>();
mockA
    .Stub(x => x.DoSomething(Arg<IC>.Is.Anything))
    .Do((Func<IC, IB>)(arg => new B(arg)))
    .Return(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rhino Mocks:如何在期望中匹配数组参数? - Rhino Mocks : How to match array arguments in an expectation? 犀牛嘲笑。 如何添加订阅事件处理程序的期望 - Rhino Mocks. How to add expectation that event handler was subscribed 如何在 Rhino Mocks 存根中设置默认返回值 - How to set a default return value in a Rhino Mocks stub Rhino Mocks:如何在不明确所有期望的情况下更改方法,属性或字段的期望? - Rhino Mocks : How to change expectation on method, property or field without clear all expectation? Rhino Mocks期望未正确返回收藏 - Rhino Mocks expectation not returning collection correctly Rhino Mocks - 通过多次调用模拟返回值发生变化(即使传递相同参数时)的方法 - Rhino Mocks - mocking a method whose return value changes (even when passed the same parameter) with multiple calls Rhino-Mocks如何嘲笑? - How Rhino-Mocks mocks? 使用Rhino Mocks,如何检查传递给模拟对象的参数中Struct字段的值? - Using Rhino Mocks how to check the value of a Struct Field in the parameter passed to a mock object? Rhino Mocks 使用存根函数的输入参数来创建返回值 - Rhino Mocks use input parameters of stubbed function to create return value Rhino Mocks的意思是“需要返回值还是抛出异常”? - What does Rhino Mocks mean by “requires a return value or an exception to throw”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM