This is the situation. I had an asynchronous call, so I needed to do an intermediate level for this to test it.
request.BeginGetResponse(new AsyncCallback(LoginCallback), requestState);
So, to test this without a real request, I created an interface that I can make fun of.
public interface IRequestSender { void Send(HttpWebRequest request, AsyncCallback internalCallback, object requestState); }
Then in the implementation, I can use a call like the one above, and I can provide some mock class to call my callback method, regardless of whether the request is valid or not. My cool class is as follows.
public class RequestSenderMock : IRequestSender { public void Send(HttpWebRequest request, AsyncCallback internalCallback, object requestState) { var result = new Mock<IAsyncResult>(); result.Setup(x => x.AsyncState).Returns(requestState); internalCallback(result.Object); } }
Now I can easily create a mock object in my unit test and use it. But when I create
var sender = new Mock<RequestSenderMock>();
I can not check the number of calls for this object.
sender.Verify(x => x.Send(It.IsAny<HttpWebRequest>(), It.IsAny<AsyncCallback>(), It.IsAny<object>()), Times.Once());
It says that my method must be virtual. Is there a way to do this without making my method virtual? It would be better if I could somehow specify the impelementation method when using the interface.
var sender = new Mock<IRequestSender>();
And something using an installation method or another to implement this layout. Than I just delete my cool class. Is it possible? What are you offering?