如何使用 Moq 创建回显模拟?
我正在为我的 ITransformer 接口创建一个模拟。
public interface ITransformer
{
String Transform( String input );
}
我可以创建一个基于特定输入返回给定字符串的模拟:
var mock = new Mock<ITransformer>();
mock.Setup(s => s.Transform("foo")).Returns("bar");
我想做的是使用 Transform()
方法创建一个模拟,该方法会回显传递给它的任何内容。我该怎么做呢?有可能吗?
我意识到我的问题可能会颠覆 Moq 和模拟一般应该工作的方式,因为我没有指定固定的期望。
我也知道我可以轻松创建自己的类来执行此操作,但我希望找到一种可以在类似情况下使用的通用方法,而不必每次都定义一个新类。
I am creating a mock for my ITransformer
interface.
public interface ITransformer
{
String Transform( String input );
}
I can create a mock that returns an given string based on a specific input:
var mock = new Mock<ITransformer>();
mock.Setup(s => s.Transform("foo")).Returns("bar");
What I would like to do is create a mock with a Transform()
method that echoes whatever is passed to it. How would I go about doing this? Is it even possible?
I realise my question might be subverting the way that Moq and mocks in general are supposed to work because I'm not specifying a fixed expectation.
I also know that I could easily create my own class to do this, but I was hoping to find a generic approach that I could use in similar circumstances without having to define a new class each time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该回显提供给该方法的任何内容。
This should echo back whatever was supplied to the method.