This is a migrated thread and some comments may be shown as answers.

Mocking an IAsyncResult (APM) pattern result.

1 Answer 199 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 14 Feb 2017, 10:53 PM
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.

1 Answer, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 15 Feb 2017, 10:25 PM

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);
Tags
General Discussions
Asked by
Mike
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Share this question
or