嘲笑委托“包装器”使用起订量

发布于 2024-10-26 09:30:38 字数 1914 浏览 1 评论 0原文

本质上,我有一个用于“安全”调用委托的类。 “安全”本质上意味着如果某个成员已经被释放,则调用将被跳过。此类的代码:

    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 技术交流群。

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

发布评论

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

评论(1

梦里南柯 2024-11-02 09:30:39

你不能这样做吗:

var operationInvoker = new Mock<IOperationInvoker<string>>();
operationInvoker.Setup(oi => oi.Invoke(It.IsAny<Action<string>>())
                .Callback((Action<string>> callback) => callback("hi"));

Can't you just do:

var operationInvoker = new Mock<IOperationInvoker<string>>();
operationInvoker.Setup(oi => oi.Invoke(It.IsAny<Action<string>>())
                .Callback((Action<string>> callback) => callback("hi"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文