Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4088304
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:57:55+00:00 2026-05-20T18:57:55+00:00

Essentially I have a class that is used for safe invocation of delegates. Safe

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:57:55+00:00Added an answer on May 20, 2026 at 6:57 pm

    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"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button class that I wrote in CSS. It essentially displays block,
I have a View that uses RenderAction to render another View - it used
What I've got are chunks of code that are used over and over again
I have a custom Fraction class, which I'm using throughout my whole project. It's
I have a roles model in a rails app that I have written a
I have a C# service that runs continuously with user credentials (i.e not as
I am looking for a type of lock where the thread that holds the
I have created a custom generic queue which implements a generic IQueue interface, which
I'm building a site that has three main types of online news publications: Article
In Scala, the interaction of overloading and implicit argument resolution seem to make it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.