How-to do unit-testing of methods involving file input output?
c++, file, stub, unit-testing
Solution
For unit-testing THIS function, you should use stubs for each of the called functions.
Each called function then has its own unit test suite, which exercises that function.
For `read_entire_file_to_buffer()`, you want at least one test file that overflows the buffer, massively, to verify that you do not crash and burn when they feed you the New York Stock Exchange histories instead of the 40-character config file you were expecting.
Problem
I'm using C++Test from Parasoft for unit testing C++ code. I came across the following problem. I have a function similar to the next one (pseudocode): ``` bool LoadFileToMem(const std::string& rStrFileName) { if( openfile(rStrFileName) == successfull ) { if( get_file_size() == successfull ) { if( read_entire_file_to_buffer() == successfull ) { return true; } return false; } return false; } return false; } ``` My questions in this case are: Should I use stubs for file system functions? Or should I include specific sample test files for running the unit tests? In my case std::fstream class is used for file input. Has anyone better suggestions? (Best if done in C++Test but not mandatory).