chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I am using typemock and trying to fake a network stream readbyte method. The fake looks right and its doing the fake and returning value but in the method under test  - the return value is modified to either 0 or -1. It may have to do with the stream pointer but not sure how to fix. The networkstream write method is faked as expected:

Method under test:

public class TcpSend


        NetworkStream m_rcmpStream;

        TcpClient m_dataClient;

        public List<Byte> Response;     

        public void SendCommand(byte[] command, String ip, String portNum)


            Response = new List<byte> ();

            IPEndPoint rcmpLocalEndPoint = new IPEndPoint(IPAddress.Parse(ip), int.Parse(portNum));

            m_dataClient = new TcpClient();


            m_rcmpStream = m_dataClient.GetStream();

            m_rcmpStream.Write(command, 0, command.Length);

            int first = m_rcmpStream.ReadByte();

            byte firstByte = (byte)first;


            while (m_rcmpStream.DataAvailable)






Test Method:



        public void SendCommandTest()


            Byte[] cmdBytes = new Byte[5] { 0x00, 0x03, 0x00, 0x0e, 0x00 };

            int[] replyBytes = new int[3] { 0x01, 0x02, 0x03 };

            int callCount = 0;

            var fakeTcpStream = Isolate.Fake.AllInstances<NetworkStream>();

            Isolate.WhenCalled(() => fakeTcpStream.ReadByte()).DoInstead(context =>


                int currentValue = replyBytes[callCount++];

                return currentValue;


            byte[] sentData = null;

            Isolate.WhenCalled(() => fakeTcpStream.Write(null, 0, 0)).DoInstead(context =>


                sentData = context.Parameters[0] as byte[];              


            Isolate.WhenCalled(() => fakeTcpStream.DataAvailable).WillReturn(true);

            Isolate.WhenCalled(() => fakeTcpStream.DataAvailable).WillReturn(true);

            Isolate.WhenCalled(() => fakeTcpStream.DataAvailable).WillReturn(false);

            var fakeTcp = Isolate.Fake.NextInstance<TcpClient>();

            Isolate.WhenCalled(() => fakeTcp.Connected).WillReturn(true);

            Isolate.WhenCalled(() => fakeTcp.GetStream()).WillReturn(fakeTcpStream);

            var target = new TcpSend();

            target.SendCommand(cmdBytes, "", "8002");

            for (int i = 0; i < cmdBytes.Length; i++)


                Assert.AreEqual(cmdBytes[i], sentData[i]);


            for (int i = 0; i < replyBytes.Length; i++)


                Assert.AreEqual(replyBytes[i], target.Response[i]);


asked ago by meansala (2.6k points)

1 Answer

0 votes
Hi Alan,

Thanks for reaching out and we are sorry for what you are experiencing.

I forwarded your issue to our development team and they are investigating.

I will keep you updated through mail.


answered ago by lilachd (300 points)