views:

56

answers:

2

Greetings,

I'm fairly new to TDD and ran across a unit test that I'm not entirely sure how to address.

Basically, I'm testing a couple of legacy class methods which read/write a binary stream to a file. The class functions take a serializable object as a parameter, which handles the actual reading/writing to the file.

For testing this, I was thinking that I would need a serialization mock object that I would pass to this function. My initial thought was to have the mock object hold onto a (char*) which would dynamically allocate memory and memcpy the data. However, it seems like the mock object might be doing too much work, and might be beyond the scope of this particular test.

Is my initial approach correct, or can anyone think of another way of correctly testing this?

Thanks!

+1  A: 

I'm sure there will be some disagreement with this, but I feel that mock objects are an advanced topic. I'd get comfortable with mockless TDD before diving into mocks. That said, there are plenty of problems that are best addressed with mocks. In this case, your mock wouldn't really need to do anything, just record that the calling function had invoked them. Test the actual reader and writer in separate tests. Your instinct that your mock is doing too much work is correct.

Carl Manaster
A: 

just use std::ostringstream so you don't have to manage the char*.

Also, since it seems that you're testing the client of the serializable object, the object doesn't actually have to serialize. You should be making sure the correct functions are called with the right parameters. You may very well not need any char* or anything like it.

Noah Roberts