I have some code like this
x = x * 1
class StringTest < Test::Unit::TestCase
require a file, you literally are "running it". That is how your test knows that
something method is defined - because it has already initialised the definition.
What you are really asking, I suppose, is how to silence the
puts command(s) when
requireing the file. There are several possible approaches for this - here are some suggestions:
puts, directly. One really crude but simple approach could be to wrap these debug messages a helper method - such as:
# something.rb: def debug(message) unless $debug_messages_disabled puts message end end def something(x) x = x * 1 end debug "something" # in your spec (spec_helper.rb?): $debug_messages_disabled = true
However, this approach does not scale well at all...
A better approach could be to use a
Logger instead of
puts. If you choose to log to a file, then your problem is already solved! And, if you insist on logging to
stdout, then you could simply increase the log level when running the tests - so long as you have a convenient way to set this log level. Something like:
# something.rb: # ... MyApplication.logger.debug "something" # NOT `puts` # config/environments/development.rb config.log_level = :debug # config/environments/test.rb config.log_level = :warn
...But that sort of approach probably way too much effort to set up for a single-file like this!
Which leads to the final, easy option to hide the output of those
puts commands: Suppress the STDOUT in your tests.
You could either achieve this by only stubbing
# spec_helper.rb before do IO.any_instance.stub(:puts) # globally YourClass.any_instance.stub(:puts) # or for just one class end
Or, for an even more general solution, you can block all STDOUT:
#spec_helper.rb RSpec.configure do |config| original_stderr = $stderr original_stdout = $stdout config.before(:all) do # Redirect stderr and stdout $stderr = File.open(File::NULL, "w") $stdout = File.open(File::NULL, "w") end config.after(:all) do $stderr = original_stderr $stdout = original_stdout end end