Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6195149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:25:06+00:00 2026-05-24T03:25:06+00:00

This is the situation. I had async call so I needed to make Mid

  • 0

This is the situation. I had async call so I needed to make Mid tier for this in order to be able to test it.

request.BeginGetResponse(new AsyncCallback(LoginCallback), requestState);

So, in order to be able to test this without real request, I created interface which I can mock.

public interface IRequestSender
    {
        void Send(HttpWebRequest request, AsyncCallback internalCallback, object requestState);
    }

Then in implementation I can use call like that one above and I can provide some mock class to call my callback method regardless request is valid or not. My mock class looks like this.

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);
        }
    }

I can now easily create mock object in my unit test and use it. But when I create

var sender = new Mock<RequestSenderMock>();

I’m unable to verify call count for this object.

sender.Verify(x => x.Send(It.IsAny<HttpWebRequest>(), It.IsAny<AsyncCallback>(), It.IsAny<object>()), Times.Once());

It says that my method needs to be virtual.
Is there a way to do this without making my method virtual? It would be best if I could somehow specify method impelementation when using interface.

var sender = new Mock<IRequestSender>();

And that somehow with Setup method or some other to make implementation on this mock object. Than I’ll simply remove my mock class.
Is this possible? What do you suggest?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:25:07+00:00Added an answer on May 24, 2026 at 3:25 am

    I find it confusing that you are creating a manual mock and using a mocking-framework to mock it (mocking a mock). I would consider moving your custom functions into some utility class and using callbacks instead.

    Example:

    public class RequestSenderHelpers
    {
        public static void Send(HttpWebRequest request, AsyncCallback internalCallback, object requestState)
        {
            var result = new Mock<IAsyncResult>();
            result.Setup(x => x.AsyncState).Returns(requestState);
            internalCallback(result.Object);
        }
    
    }
    
        [Test]
        public void Callback_VerifyingWithMethodImplementation_VerifyWorks()
        {
            // arrange
            var sender = new Mock<IRequestSender>();
            sender.Setup(s => s.Send(It.IsAny<HttpWebRequest>(), It.IsAny<AsyncCallback>(), It.IsAny<object>())).Callback<HttpWebRequest, AsyncCallback, object>(RequestSenderHelpers.Send);
    
            // act
            sender.Object.Send(null, delegate {}, null);
    
            // assert
            sender.Verify(s => s.Send(It.IsAny<HttpWebRequest>(), It.IsAny<AsyncCallback>(), It.IsAny<object>()));
        }
    

    To avoid the verbose setup you can wrap the setup of the method in an extension method and change your test accordingly:

    public static class RequestSenderHelpers
    {
        public static void Send(HttpWebRequest request, AsyncCallback internalCallback, object requestState)
        {
            var result = new Mock<IAsyncResult>();
            result.Setup(x => x.AsyncState).Returns(requestState);
            internalCallback(result.Object);
        }
    
        public static void SetupSendWithMockedAsyncResult(this Mock<IRequestSender> sender)
        {
            sender.Setup(s => s.Send(It.IsAny<HttpWebRequest>(), It.IsAny<AsyncCallback>(), It.IsAny<object>())).Callback<HttpWebRequest, AsyncCallback, object>(Send);            
        }
    
    }
    
        [Test]
        public void Callback_VerifyingWithMethodImplementation_VerifyWorks()
        {
            // arrange
            var sender = new Mock<IRequestSender>();
            sender.SetupSendWithMockedAsyncResult();
    
            // act
            sender.Object.Send(null, delegate {}, null);
    
            // assert
            sender.Verify(s => s.Send(It.IsAny<HttpWebRequest>(), It.IsAny<AsyncCallback>(), It.IsAny<object>()));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

OK. This is a bit of a vanity app, but I had a situation
Ok, So I have the following situation. I originally had some code like this:
I've had this situation occur a number of times in the library I'm writing,
This situation arises from someone wanting to create their own pages in their web
In this situation I am trying to perform a data import from an XML
In this situation I have two models, Comment and Score. The relationship is defined
I ran across this situation this afternoon, so I thought I'd ask what you
Look at this situation: www.websitea.com displays an img tag with a src attribute of
I've come upon this situation a few times before. I wish to reference a
I frequently encounter this situation in my VB6 applications Private Sub DoSomething On Error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.