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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:56:50+00:00 2026-06-07T05:56:50+00:00

Let’s say I have some APM (BeginXxx, EndXxx) pattern async methods (as part of

  • 0

Let’s say I have some APM (BeginXxx, EndXxx) pattern async methods (as part of some WCF service proxy i’m calling):

public interface ISomeService
{
    IAsyncResult BeginSomeMethod(int num, AsyncCallback callback, object state);
    int EndSomeMethod(IAsyncResult ar);
}

My actual code uses uses the Task.Factory.FromAsync to create a Task, and then awaiting this task using the new async/await pattern introduced in .net 4.5.

I would like to test my class and thus I need to write a method that receives the mock, begin method, end method and return value and sets up the mock so that it would eventually return the required return value.

example usage:

SetupAsync(mock, mocked => mocked.BeginSomeMethod, mocked=> mocked.EndSomeMethod, 7);

Which will cause an async flow with any int argument to return 7.
I cannot seem to figure out how to accomplish such a thing in moq.

  • 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-07T05:56:51+00:00Added an answer on June 7, 2026 at 5:56 am

    First, I recommend that you use the TaskWsdlImportExtension to create Task-based asynchronous WCF proxies. VS2012 does this by default, but you have to set it up yourself on VS2010+AsyncCTP. It’s easier to unit test against a Task API.

    If you do want to unit test against Begin/End, I think the easiest way would be to use Task-based mocks and expose Begin/End endpoints. The [AsyncFactory|AsyncFactory<T>].[ToBegin|ToEnd] methods in my AsyncEx library provide Begin/End method wrappers around a Task, or you can see Stephen Toub’s blog post about writing these kinds of wrappers.

    You can get simple already-completed tasks from Task.FromResult, or you can use the following construct to force an asynchronously-completed task:

    Task.Run(async () => { await Task.Yield(); return 7; });
    

    (the Async CTP equivalent would be):

    TaskEx.RunEx(async () => { await TaskEx.Yield(); return 7; });
    

    I’m not entirely sure how to tie this into Moq. Again, I suspect a Task-based API would be easier to mock than Begin/End. Begin/End has its own special semantics (you have to pass the correct IAsyncResult, End must be called exactly once for each Begin, etc), so there’s more stuff to test.

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say I have some json like this in mongo: {n:5} and a java
Let's say we have such code as below 'Call service Set xmlhttp_ = CreateObject(MSXML2.xmlhttp)
Let's say I have a method in java, which looks up a user in
Let me explain best with an example. Say you have node class that can
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords

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.