Mocking an IAsyncResult (APM) pattern result.

2 posts, 0 answers
  1. Mike
    Mike avatar
    14 posts
    Member since:
    May 2012

    Posted 14 Feb Link to this post

    I get how to Mock the Begin method in the pattern, but I cannot find any documentation that shows how to mock the End method or how to handle the Callback. I would love to see an example of how to do this.
  2. Mike
    Mike avatar
    14 posts
    Member since:
    May 2012

    Posted 15 Feb Link to this post

    I've figured this out on my own. The key part I was missing properly defining the .DoInstead to invoke the c

    Here's what I put together.

    01.IAsyncResult asyncResult = Mock.Create<IAsyncResult>();
    02.var ldapConnection = Mock.Create<LdapConnection>();
    03.Mock.Arrange(
    04.    () =>
    05.        ldapConnection.BeginSendRequest(Arg.IsAny<DirectoryRequest>(), Arg.IsAny<PartialResultProcessing>(),
    06.            Arg.IsAny<AsyncCallback>(), null))
    07.            .DoInstead<DirectoryRequest, PartialResultProcessing, AsyncCallback, object>((dr, prp, cb, os) => { cb.Invoke(asyncResult); })
    08.            .Returns(asyncResult);
    09.Mock.Arrange(() => ldapConnection.EndSendRequest(asyncResult)).Returns(searchResponse);
Back to Top