These last few days, I’ve been working on a program that unlike many of my projects, has data that won’t sit still.
There are tons of network links, socket connections and data sets that I don’t have any control over; it’s incredibly difficult to unit-test. How do you test a socket connection when the data the server is sending to you keeps changing? I realized that there are lots of network programming tasks where I don’t even know how start unit testing, and sometimes, when I can test, I have to set up dummy servers to replicate the data, it’s incredibly painful and it feels wrong.
How do you go about unit testing these kinds of applications?
Related posts:





This is precisely the case where you need to use mock objects for testing.
You need to be able to write your app given your assumptions (whether they are right or wrong). Your app talks to the sockets, so you have to introduce a facade that hides that fact. During testing you mock it out to provide data that you think is realistic. You will likely have to adapt this as the data changes, but it allows you to move forward.
This is also a good opportunity to put in some effective logging to notify you if data comes in that is not recognized.
NMock2 is a cool mock objects framework, as is RhinoCommons. They approach syntax a bit differently, so it’s a matter of preference really.
Ben Scheirman - 8 Nov 06 at 1:09 am