A bit verbose, are we?
While “debugging” a Rails application, developers tend to use the puts method to print debug information while developing. Although perhaps useful for the developer in mind, it makes it a lot harder for colleagues to use testing in their work as the console is cluttered with debug information.
Without going into whether using puts for debugging is a good practice or not, I wrote some Ruby code today that silences all this information while I’m running my tests. A very simple analysis of the problem tells me that:
- I only want the output silenced when running my tests. I found it tempting to force my policy of not using puts on my peers, but strictly spoken all I wanted was my tests to run less verbosely
- The reason for all this turning up in the console is the use of the method puts
The first point tells me that testhelper.rb is the natural place to put this functionality. This file is always loaded when you run your tests. The second point tells me that the method I should target is the puts method – and the puts calls are made in ActiveRecord::Base subclasses. So my first try was to put the following into testhelper.rb:
module ActiveRecord
class Base
def puts(something)
# let's eat it
end
end
end
Running my tests, I was pleased to see about 50% of the output silently eaten by this hack. However, there was still some output sent from classes not extending ActiveRecord::Base, so I needed to be a bit more greedy. Let’s put it all into a module:
module SilentOutput
def puts(something)
#
end
end
module ActiveRecord
class Base
include SilentOutput
end
end
class SomeOtherClass;include SilentOutput;end
There! Now I can get back to work. One last issue before I let you get back to work: How do you discover which classes called the puts method, so you know which ones to include? A small trick is to put the following into your test helper file, and it will reveal other classes calling the method:
Kernel.module_eval do
alias_method :old_puts, :puts
def puts(something)
raise "WARNING: #{self.class.to_s} tried to call puts with the message #{something}"
end
end
These lines mix into Kernel and creates an alias to the (old) puts method – in case you need it actually puts-ing, and then raises an error revealing which class tried to use puts to print its output. This gives you a hint of which further classes may be candidates for including your SilentOutput module.

Such a superbly written post.. Thnkx after sharing this post! <IMG>http://www.sedonarapidweightloss.com/weightloss-diet/34/b/happy.gif</IMG>
av Anonym, 15/12-2010
Na Leute, Nachdem ich in in den letzten Tagen bei mehreren Communities nachgeschaut habe, kam ich in Folge der positiven Ansichten die da beschrieben werden, dazu mich bei http://www.homepages-eintragen-suchmaschine.de eintragen zu lassen. Meine Webseite, wurde dann nach einiger Zeit bei google und noch weiteren Suchmaschinen angemeldet. Mir hat es aufjedenfall viel gebracht ! Als meine Internetseite bei den Suchmaschinen angemeldet wurde, ist meine Besucheranzahl stetig besser geworden. Daher kann Ich euch eine Anmeldung mit guten Gewissen ans Herz legen. K?nnt es ja mal testen Bis bald. Eure, Valeria
av sminnaopponse, 23/12-2010