TDD和Mocking TcpClient

Jam*_*pen 10 c# tdd networking mocking

人们如何处理模拟TcpClient(或TcpClient之类的东西)?

我有一个接收TcpClient的服务.我应该把它包装成更可模仿的东西吗?我该怎么做呢?

Dor*_*oby 26

当来到非测试友好的模拟类(即密封/未实现任何接口/方法不是虚拟的)时,您可能希望使用适配器设计模式.

在此模式中,您将添加一个实现接口的包装类.然后,您应该模拟接口,并确保所有代码都使用该接口而不是不友好的具体类.它看起来像这样:

public interface ITcpClient
{
   Stream GetStream(); 
   // Anything you need here       
}
public class TcpClientAdapter: ITcpClient
{
   private TcpClient wrappedClient;
   public TcpClientAdapter(TcpClient client)
   {
    wrappedClient = client;
   }

   public Stream GetStream()
   {
     return wrappedClient.GetStream();
   }
}
Run Code Online (Sandbox Code Playgroud)


cas*_*ora 5

我认为@Hitchhiker走在正确的轨道上,但我也想考虑将这样的事情进一步抽象出来.

我不会直接嘲笑TcpClient,因为即使你已经编写了测试,这仍然会使你与底层实现过于紧密.也就是说,您的实现特别依赖于TcpClient方法.就个人而言,我会尝试这样的事情:

   [Test]
    public void TestInput(){

       NetworkInputSource mockInput = mocks.CreateMock<NetworkInputSource>();
       Consumer c = new Consumer(mockInput);

       c.ReadAll();
    //   c.Read();
    //   c.ReadLine();

    }

    public class TcpClientAdapter : NetworkInputSource
    {
       private TcpClient _client;
       public string ReadAll()
       { 
           return new StreamReader(_tcpClient.GetStream()).ReadToEnd();
       }

       public string Read() { ... }
       public string ReadLine() { ... }
    }

    public interface NetworkInputSource
    {
       public string ReadAll(); 
       public string Read();
       public string ReadLine();
    }
Run Code Online (Sandbox Code Playgroud)

此实现将完全将您与Tcp相关的详细信息(如果这是设计目标)分离,您甚至可以从硬编码值集或测试输入文件中输入测试输入.如果你正在考虑长期测试你的代码,那就非常有用了.