How can I develop a testable TcpClient / TcpListener Wrapper

前端 未结 2 588
情话喂你
情话喂你 2021-01-14 07:28

i want to develop a testable TcpClient / TcpListener wrapper. I want to be able to mock the incoming and outgoing data.

I want to do this because i have higher tier

2条回答
  •  灰色年华
    2021-01-14 07:58

    You could use the Decorator Pattern

    • Make your very own class that just wraps the TcpClient
    • This class simply does pass through calls to functions in TcpClient.
    • An overloaded constructor to this class could accept an instance of an actual tcp client which it would wrap, if creating one is involved.
    • Extract the interface so your new class should implement the interface ITcpClient
    • Update all your dependencies to use the new interface ITcpClient
    • Your new interface is now mockable, inject your mocks were appropriate and test away :)

    Repeat the same for TcpServer.

提交回复
热议问题