How to customize the BeginXXX EndXXX method call using moq?

Let's say I have some asynchronous APM template methods (BeginXxx, EndXxx) (as part of some WCF service proxy that I call):

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

In my actual code, it is used Task.Factory.FromAsyncto create a Task, and then waits for this task using the new async / await template introduced in .net 4.5.

I would like to test my class, and so I need to write a method that gets the mock, begin, end method and return method and sets the layout so that it ultimately returns the required return value.

usage example:

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

Which will cause an asynchronous stream with any int argument to return 7. I cannot figure out how to do this in the mock.

+5
source share
1

-, TaskWsdlImportExtension WCF- Task. VS2012 , VS2010 + AsyncCTP. unit test API Task.

unit test Begin/End, , mocks Task Begin/End. [AsyncFactory|AsyncFactory<T>].[ToBegin|ToEnd] AsyncEx Begin/End Task, .

Task.FromResult, :

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

( Async CTP):

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

, Moq. , , API Task , Begin/End. Begin/End ( IAsyncResult, End Begin ..), .

+6

All Articles