From: Martin Hansen Date: 2010-05-11T17:17:38+09:00 Subject: Re: Unit Test of method calling system() - how? @Caleb > I would extract a helper from both methods which does the bulk of the > work (the first 2 lines) but does not exit. Then test the helper and > don't bother testing the methods that call it. Hm, I thought all this unit testing was about testing the interface - these are private methods. But perhaps this is one of those rare cases where you have to do all sorts of tricks? I was wondering if a special class for printing usage would be in order? > You then have the problem that this print_wiki command is printing > something to stdout, which you want to test. Either capture stdout in > the test and verify it contains what you want, or use backticks > (`...`) instead of system in your helper method. Actually, 'print_wiki' outputs to stderr - which makes sense if you are executing a command and piping the output to a file. You want to see the usage info as an indicator that your did something wrong (if you did mess up), and not have the usage information in the output file! > Since print_wiki might change its behavior, you might think about > making the assertion that tests print_wiki's output string somewhat > loose.... just verify that some expected substring occurs in there > somewhere, rather than verifying that it is exactly equal to some > expected string. Yup, that makes sense. > $0 is also going to present you some problems in a test... it will not > be the same value in the test as it is when run from your main > program. Not sure what to suggest about that without more context. Better to supply the base name of the script as an argument? > 3 other points: > > looks like both methods have an unintended extra trailing + on their > first line No??? On my screen they end with + ".wiki". > standard style is to use 2 spaces for indentation; you appear to be > using 8 I do use a tab space of 2, however, this is the result of pasting from vi and then tabs are expanded wildly (I have no solution for this). > since the 2 methods are nearly identical, you should pull out the > common code. Sure. Cheers, Martin -- Posted via http://www.ruby-forum.com/.