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 8904835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:14:20+00:00 2026-06-15T02:14:20+00:00

In my class, i have declared a delegate type, I am creating an instance

  • 0

In my class, i have declared a delegate type, I am creating an instance of it, and then calling BeginInvoke off of that instance:

public class ClassA
{
    ...
    public delegate bool MyDelegate(int x);
    ...
    public void MethodA()
    {
        ...
        var myDelegate = new MyDelegate(Foo);
        myDelegate.BeginInvoke(...);
        ...
    }
    ...
}

In writing unit tests for the MethodA, I don’t actually ever want BeginInvoke to be called. What I want to do is use the Moles framework to mole out the BeginInvoke call. Normally, I’d try

MMyDelegate.AllInstances.BeginInvoke... = (...) => { /* something here */ }

but I have been unable to get moles to generate anything for the MyDelegate type.

According to http://msdn.microsoft.com/en-us/library/system.delegate.aspx, “The common language runtime provides each delegate type with BeginInvoke and EndInvoke methods”. If this is the case, and BeginInvoke isn’t even a method within the Delegate class (being provided by the CLR), is it even possible to mole it? Has anyone else had any luck in moling out BeginInvoke off of delegate types?

  • 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-06-15T02:14:22+00:00Added an answer on June 15, 2026 at 2:14 am

    I don’t really know about Microsoft Moles but I tried to achieve the same with Microsoft Fakes (actually this is a heir of Microsoft Moles built in into the Visual Studio 2012 Premium and Ultimate).

    When I tried to build Fake assembly (know Microsoft calls it Fakes and Shims instead of Moles and Stubs) I got following warning:

    Warning 1   Cannot generate stub for AssemblyUnderTest.FooDelegate: type is sealed. d:\Sources\Projects\PlayingWithMoles\PlayingWithMoles\Fakes\AssemblyUnderTest.fakes
    Warning 2   Cannot generate shim for AssemblyUnderTest.FooDelegate: type is a delegate. d:\Sources\Projects\PlayingWithMoles\PlayingWithMoles\Fakes\AssemblyUnderTest.fakes
    

    (To get this warning you should turn off additional diagnostic in the fakes configuration file (I’m not sure that format is the same in Fakes and Moles):

    <Fakes xmlns="http://schemas.microsoft.com/fakes/2011/" **Diagnostic="true"**>
      <Assembly Name="AssemblyUnderTest"/>
    </Fakes>
    

    But alas, this warning clearly stated that we can’t do this. But we can avoid this issue at all by moving to another asynchronous API, for example to Task Parallel Library a.k.a. TPL.

    Task (or Task of T) object itself represents asynchronous operation as a first class object that allows you to store this “async operation” in the field, return it from method or pass it around. This improves testability a lot because you can easily mock you implementation with fake one:

    class SomeService
    {
      Task<string> GetSomeResult()
      {
        // Performing long-running operation to obtain the result
      }
    }
    
    class YourServiceConsumer
    {
      private void YourMethod()
      {
         Task<string> task = service.GetSomeResult();
      }
    }
    

    Than you can easily extract your dependency with separate interface and inject it into you YourServiceConsumer or fake it using Microsoft Moles (or Microsoft Fakes) without adding additional layers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need help calling event from another class. I have class with declared event: public
I have declared a class as class DCFrameListener : public FrameListener, public OIS::MouseListener, public
WorkAround: I have declared a class level Public static variable and initialized with a
I have a class declared as: public class Foo<T> : Panel where T :
I have a problem in calling a template class I have. I declared a
I have a class with a delegate declaration as follows... Public Class MyClass Public
I have a class MyCLController with a property dataSource that is data source delegate
I have a class, Plotter , which has a static event (declared public static
I'm creating an instance of a class called S3ObjectController (S3OC) that has one method
In my main class 'A' I have declared a function and delegate to call

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.