c# - How to setup a BeginXXX EndXXX method call with moq? -
let's have apm (beginxxx, endxxx) pattern async methods (as part of 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 task.factory.fromasync create task, , awaiting task using new async/await pattern introduced in .net 4.5.
i test class , need write method receives mock, begin method, end method , return value , sets mock return required return value.
example usage:
setupasync(mock, mocked => mocked.beginsomemethod, mocked=> mocked.endsomemethod, 7); which cause async flow int argument return 7. cannot seem figure out how accomplish such thing in moq.
first, recommend use taskwsdlimportextension create task-based asynchronous wcf proxies. vs2012 default, have set yourself on vs2010+asyncctp. it's easier unit test against task api.
if want unit test against begin/end, think easiest way use task-based mocks , expose begin/end endpoints. [asyncfactory|asyncfactory<t>].[tobegin|toend] methods in asyncex library provide begin/end method wrappers around task, or can see stephen toub's blog post writing these kinds of wrappers.
you can simple already-completed tasks task.fromresult, or can use following construct force asynchronously-completed task:
task.run(async () => { await task.yield(); return 7; }); (the async ctp equivalent be):
taskex.runex(async () => { await taskex.yield(); return 7; }); i'm not entirely sure how tie moq. again, suspect task-based api easier mock begin/end. begin/end has own special semantics (you have pass correct iasyncresult, end must called once each begin, etc), there's more stuff test.
Comments
Post a Comment