From: Martin Hansen Date: 2010-05-12T21:55:00+09:00 Subject: Re: Unit Test of method calling system() - how? > AFAICS this will always raise, since $? will never be nil. I think you > want: > > raise ... unless $?.success? Aaah, nice trick. I did notice that this always was raising, but I was postponing forensics until RTFM :o) So with a change of code (http://pastie.org/956910), including an explicit file check, I now have this working: test "Options.parse with empty argv and missing wiki file raises" do opt_parser = Options.new assert_raise() { opt_parser.parse([], "foo") } end > Well, that's what mocking is for. You say you don't actually want to run > print_usage_and_exit, so I think you want to check that it *would* have > been run, with the correct arguments to do what you want. > > opt_parser = Options.new > opt_parser.expects(:print_usage_and_exit).with() > assert_nothing_raised { opt_parser.parse([], "/foo/bar.rb") } Right, so this appears to be working nicely: test "Options.parse with empty argv and existing wiki file don't raise" do opt_parser = Options.new opt_parser.expects(:print_usage_and_exit).with() assert_nothing_raised { opt_parser.parse([], SCRIPT_PATH) } end > At the end of your test method, if :print_usage_and_exit wasn't called, > or was called with different arguments, you'll get an error raised by > the mocking framework. The awkward thing is that print_usage_and_exit > might actually be called multiple times, so it may be worth changing the > call to > > if print_usage_full? > print_usage_and_exit(true) > return # not reached except in testing > end This I don't understand. How can the method be called multiple times, and how can that be fixed by introducing a return? I do get this funky error: "- expected exactly once, already invoked once: #.print_usage_and_exit(:full)" from this (the case where print_usage_and_exit should be invoked with an arg): test "Options.parse with --help in argv and existing wiki output long usage" do opt_parser = Options.new opt_parser.expects(:print_usage_and_exit).with(:full) assert( "" == opt_parser.parse(["--help"],SCRIPT_PATH) ) end and I don't know what to assert. > As for $0: this tends to be dangerous anyway (it is subject to > truncation under various conditions). __FILE__ is safer, giving the > location of the current source file, if that's usable. Yup, $0 is now gone. Cheers, Martin -- Posted via http://www.ruby-forum.com/.