On 10/ 4/11 11:37 AM, nroberts wrote:
> On Oct 3, 3:30 pm, Jorgen Grahn<grahn+n...@snipabacken.se> wrote:
>> On Mon, 2011-10-03, nroberts wrote:
>>> On Oct 3, 1:27 pm, James Kuyper<jameskuy...@verizon.net> wrote:
>> ...
>>>> Testability shouldn't have much affect on your code design. If it does,
>>>> you're doing something wrong, either in your design or your testing.
>>
>>> I can't say I agree with that at all. One could argue of course that
>>> sure, in fact by definition it is correct since one major bonus of
>>> doing unit testing is that it forces good design, and one main reason
>>> why legacy code in notoriously untestable is because it lacked this
>>> force.
>>
>> I partly agree and partly disagree with that. Partly disagree, because
>> there are designs which make perfect sense for the program itself, but
>> are impossible for unit testing.
>>
>> For example, I believe doing I/O (file I/O, sockets, logging,
>> whatever) in the core of your code isn't necessarily bad design. If
>> you try to split it out, you end up with a complicated -- but
>> unit-testable -- design.
>
> That's where mocking and DI comes in. For example with regard to file
> I/O my C++ functions take streams. You can then set them up with
> string streams during testing.
>
> Unfortunately I've not seen a good way to do this in C so functions
> that do file I/O...only way I've found to test them is to create files
> with the appropriate content, inject the file or FILE*, and delete
> after.
It is pretty simple to interpose I/O library functions in either C or
C++. It is actually more straightforward with C; in C++ if a function
*creates* a file stream to output data, you either have to mock the
whole stream class, or know which lower level functions will be called.
Higher levels of abstraction make for more complex mocks.
> Those are all the hard cases though. They are testable and the
> designs you create to make them so are demonstratively better, but
> they are difficult problems.
No, they are quite simple once you know how!
--
Ian Collins
|