I don’t know what the right answer is, so any insight would be appreciated. From Googling around, I came across this thread on StackOverflow which talks about Validating exits and aborts in Rspec. I have a few issues with this, namely that I am using shoulda and I don’t want to validate the exit, I want to stop it from happening inside of the test.
I have figured out that rescueing a Kernel#exit with afford the same opportunity to test results so I am not sure if mocking it is necessary. Consider the following code which doesn’t involve mocking:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Application def thread_cleanup(threads) threads.each do |thread| thread.kill end $stderr.puts "Terminating..." exit end end # test cases class ApplicationTest < Test::Unit::TestCase should "kill all threads and exit cleanly" do threads = Array.new threads.push(Thread.new { sleep }, Thread.new { sleep }) begin Application.thread_cleanup(threads) rescue SystemExit => e assert_equal 0, e.status end end end |
Here we rescue the exit and continue on with the testing. The tests don’t actually exit (which allows the suite to continue) and no mocking of core functionality is required. Is it the better way? I don’t know, but it works.
The post Should I Mock Kernel#exit appeared first on Live. Interesting. Stories.