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

  • SEARCH
  • Home
  • 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 6576631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:32:10+00:00 2026-05-25T15:32:10+00:00

I am trieing to mock the receive method of a socket. Here is workaround

  • 0

I am trieing to mock the receive method of a socket.

Here is workaround that i use to get the mock to work.

public class Test_MockSocket : Socket
{
    public Test_MockSocket() : base (
                    (new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30000)).AddressFamily,
                    (SocketType.Stream),
                    (ProtocolType.Tcp))
    {
    }
}

and this is the current version of the test:

   [Test]
    public void ShouldReadFromSocket()
    {
        byte[] thingy = null;
        var Mock = new Mock<Test_MockSocket>();
        Mock.Setup(foo => foo.Receive(It.IsAny<byte[]>())).Returns(1);
        Socket MockSocket = (Socket)Mock.Object;
        Assert.AreEqual(1, MockSocket.Receive(thingy));
    }

i want to mock a receive process with this, however i fail just trying this simple response. The failure is “System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: foo => foo.Receive(It.IsAny())”

why is my setup wrong and what do i need to change to return a textstring as the byte[] buffer parameter.

  • 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-25T15:32:11+00:00Added an answer on May 25, 2026 at 3:32 pm

    You can only mock virtual methods and properties on a class. You can mock any member on an interface. The Receive method on the Socket class is not declared virtual, so Moq can’t inject behavior in the code.

    Because the .NET framework classes are usually not ideal for mocking, my understanding (which, admittedly, is limited), is that you want to create an abstraction layer for your communication. So instead of dealing directly with the socket, you would deal with your own set of interfaces and classes that hide all of the complexity of your communication. Not only does it make is easier to mock, it makes your communication easily interchangeable with other technologies, since your classes are loosely coupled.

    Interfaces are far easier to moq than classes, that is, unless your class is an abstract class, or a base class with default implementations and virtual members for extension.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm triing to get a simple application to work on symbian (Nokia N97) I
I'm triing to get soap to work in qt i found: qtsoap-2.7_1-opensource my request
Here is a question that I have to face a lot when doing the
I am using below code to get the Audio from library for triming here
I'm using the PDO class but I'm triying to remove all chars except...: function
I've created a window dynamically that's is in memory but is not visible yet.
Hi I triying to work with TinyMCE version 3.4.9 in Cakephp 1.3.14 i see
I'm triying to make a function that add a 'where' clause to a query
What I'm triying to accomplish is that everything there's a p tag with word
I'm triying to get a folder size by doing: var FolderFile:File = new File(file:///SomePath/Folder);

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.