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 6947909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:45:12+00:00 2026-05-27T13:45:12+00:00

I want to unit test the code below. I’ve been working with MSTest and

  • 0

I want to unit test the code below. I’ve been working with MSTest and I tried to learn Microsoft Moles and RhinoMocks. But I couldn’t make neither of them help me. I know I can change the code drastically to use interfaces that make it more testable, but it would require me to code interfaces and implementations that encapsulate TcpClient, NetworkStream, StreamWriter and StreamReader.

I’ve already written integration test for this and I guess that someone proficient with moles can do unit tests for this quite easily without changing the code.

using (TcpClient tcpClient = new TcpClient(hostName, port))
using (NetworkStream stream = tcpClient.GetStream())
using (StreamWriter writer = new StreamWriter(stream))
using (StreamReader reader = new StreamReader(stream))
{
    writer.AutoFlush = true;
    writer.Write(message);
    return reader.ReadLine();
}
  • 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-27T13:45:13+00:00Added an answer on May 27, 2026 at 1:45 pm

    Keep it simple.

    Abstract away the network layer. I usually use an interface called something like INetworkChannel that looks something like this:

    public interface INetworkChannel : IDisposable
    {
        void Connect(IPEndPoint remoteEndPoint);
        void Send(byte[] buffer, int offset, int count);
    
        public event EventHandler Disconnected;
        public event EventHandler<ReceivedEventArgs> Received;
    }
    

    It makes it easy to test everything and you could create SecureNetworkChannel class which uses SslStream or FastNetworkChannel which uses the new Async methods.

    The details like what stream is used or if you use TcpClient or Socket should not matter to the rest of the application.

    Edit

    Testing the INetworkingChannel implementation is easy too since you now got a class with a very clear responsibility. I do create a connection to my implementations to test them. Let the TcpListener listen on port 0 to let the OS assign a free port.

    I just make sure that it handle sends and receives properly and that it do proper clean up when a connection is closed/broken.

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

Sidebar

Related Questions

I want to Unit Test my application which use MSMQ but i found no
I want to unit test my code which will have to create a .java
I have the below class I want to unit test: public class MyClass {
I've got some code dealing with HTTP requests and I want to unit-test it.
I want to unit test my code without a dependency on having the back
We want to unit-test http-proxy related code. Is there a Java package which can
I want to unit test a RESTful interface written with Apache CXF. I use
I have a method CreateAccount(...) that I want to unit test. Basically it creates
I want to write a unit test for a Django manage.py command that does
I want to create a unit test for integration testing. What class should I

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.