I have created a component that creates a std::net::TcpStream
and over its lifetime of course reads from it and writes to it. Now I would like to write tests for it; does it connect to the right address? Does it send the correct data? Can it handle receiving invalid data? However, as I have only started using Rust, like, three days ago, I am unsure as to how to proceed. In Java (which I have been doing substantially longer than Rust) I would have at least the following possibilities:
- Create a test TCP server to collect data sent to it and to control what is being sent back.
- Extract the I/O operations into a trait, implement that trait for
TcpStream
and aTestTcpStream
, and use theTestTcpStream
during tests.
Does Rust offer something else that I don’t yet know about?
(To clarify a bit: I’m not looking for how to do any of these things, I can probably figure that out, I was asking for more possibilities than the two I have listed.)