嘲笑委托“包装器”使用起订量
本质上,我有一个用于“安全”调用委托的类。 “安全”本质上意味着如果某个成员已经被释放,则调用将被跳过。此类的代码:
public class SafeOperationInvoker<TResponse> : IOperationInvoker<TResponse>
where TResponse : class
{
private readonly IDisposableResource _callbackOwner;
private readonly IOperationInvoker<TResponse> _next;
public SafeOperationInvoker(IDisposableResource callbackOwner, IOperationInvoker<TResponse> next)
{
_callbackOwner = callbackOwner;
_next = next;
}
public void Invoke(Action<TResponse> completedCallback)
{
//wrap the callback
_next.Invoke(response => SafeOperationCompleted(response, completedCallback));
}
private void SafeOperationCompleted(TResponse response, Action<TResponse> completedCallback)
{
//only invoke the callback if not disposed
if (_callbackOwner != null && _callbackOwner.IsDisposed)
{
return;
}
completedCallback(response);
}
}
我想要的是测试 SafeOperationCompleted 方法 - 如果回调Owner 已释放,则completedCallback 不会触发(反之亦然)。
我手工创建了一个假的,使我的测试功能正确:
private class FakeOperationInvoker : IOperationInvoker<string>
{
public void Invoke(Action<string> completedCallback)
{
completedCallback("hi");
}
}
测试:
[TestMethod]
public void SafeOperationCompleted_OriginalCallback_Invoked()
{
int called = 0;
var mockOwner = new Mock<IDisposableResource>();
mockOwner.Setup(m => m.IsDisposed).Returns(false);
var invoker = new SafeOperationInvoker<string>(mockOwner.Object, new FakeOperationInvoker());
invoker.Invoke((o) => { called++;});
Assert.AreEqual(1, called, "Original callback should have been called");
}
我想做的是使用起订量创建一个模拟,其行为与 FakeOperationInvoker 的行为相同。我怎样才能实现这个目标?
Essentially I have a class that is used for "safe" invocation of delegates. "Safe" essentially means that if a certain member is already disposed, the invocation will be skipped. The code for this class:
public class SafeOperationInvoker<TResponse> : IOperationInvoker<TResponse>
where TResponse : class
{
private readonly IDisposableResource _callbackOwner;
private readonly IOperationInvoker<TResponse> _next;
public SafeOperationInvoker(IDisposableResource callbackOwner, IOperationInvoker<TResponse> next)
{
_callbackOwner = callbackOwner;
_next = next;
}
public void Invoke(Action<TResponse> completedCallback)
{
//wrap the callback
_next.Invoke(response => SafeOperationCompleted(response, completedCallback));
}
private void SafeOperationCompleted(TResponse response, Action<TResponse> completedCallback)
{
//only invoke the callback if not disposed
if (_callbackOwner != null && _callbackOwner.IsDisposed)
{
return;
}
completedCallback(response);
}
}
What I want is to test the SafeOperationCompleted method--if the callbackOwner is disposed, the completedCallback does not fire (and vice versa).
I have created a fake by hand that makes my test function correctly:
private class FakeOperationInvoker : IOperationInvoker<string>
{
public void Invoke(Action<string> completedCallback)
{
completedCallback("hi");
}
}
The test:
[TestMethod]
public void SafeOperationCompleted_OriginalCallback_Invoked()
{
int called = 0;
var mockOwner = new Mock<IDisposableResource>();
mockOwner.Setup(m => m.IsDisposed).Returns(false);
var invoker = new SafeOperationInvoker<string>(mockOwner.Object, new FakeOperationInvoker());
invoker.Invoke((o) => { called++;});
Assert.AreEqual(1, called, "Original callback should have been called");
}
What I would like to do is use moq to create a mock that behaves the same way that FakeOperationInvoker behaves. How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能这样做吗:
Can't you just do: