Read my latest article: 8 things I look for in a Ruby on Rails app (posted Thu, 06 Jul 2017 17:59:00 GMT)

Rails, Logger, and those pesky Tests

Posted by Wed, 25 Jan 2006 22:06:00 GMT

7 comments Latest by Jacolyte Fri, 05 Feb 2010 23:38:55 GMT

First of all, I would like to thank all of you who took a moment to gather around the campfire with me and share your stuff. That was much appreciated.

So, I started playing with the idea of logging in unit and functional tests and I quickly realized that calling wasn’t an option. What’s the deal-e-o? Before I gather up my posse and conspire a train hijacking, I decided that I would see how quickly I could solve my problem before I called you all out to the Rio Rails Grande for an old-fashioned shoot out. Ya know, the kind that results in a Bon Jovi song and a little blood.

You’re all in luck because:
  • A) my holster was eaten by my dog and
  • B) I found a quick solution to clean up this logger situation.

Okay… are you ready?

I know… this is totally groundbreaking!!!

Open up test/test_helper.rb and add the method… logger.

# other stuff at the top of the file... just 
# look down below at def logger
class Test::Unit::TestCase
  self.use_transactional_fixtures = true
  self.use_instantiated_fixtures  = false

  # here... look here! right below this
  def logger

Save that and pick up your pistol…

The next step is to call the logger method in your unit and functional tests.

def test_the_obvious 'asserting that 1 is 1' )
  assert 1, 1

I posted this on Rails Weenie as well.

Get help with your Rails project

comments powered by Disqus

Leave a response

  1. Avatar
    Pat Wed, 25 Jan 2006 22:35:22 GMT

    Maybe you posted this in another blog, but I’m curious as to why you’d want logging in unit tests. It’s not like you’re getting diagnostic info from your app in development mode that you don’t want filling up your production logs..simple puts statements should do the trick in unit tests, shouldn’t they?

  2. Avatar
    Damien Tanner Thu, 26 Jan 2006 11:26:02 GMT

    My god the ground is breaking beneath my feet, only yesterday was I wondering how to do this.

  3. Avatar
    Mitch Fri, 27 Jan 2006 15:13:51 GMT

    I am also wondering why logging would be important in tests. I use $stdout.puts statements right now if I need to know anything.

    But that was a smart idea ;)

  4. Avatar
    Robby Russell Fri, 27 Jan 2006 16:49:54 GMT Recommend me on Working with Rails

    I find that when I’m testing some more complex units of test and want to get some information about what is happening before and after each database interaction… having some of my own text help break things out gives me clarity. :-)

  5. Avatar
    logger Sun, 29 Jan 2006 15:16:43 GMT

    You want logging in unit tests so you can help debug what went wrong when a problem is found. Logging is for always.

  6. Avatar
    Victor Cosby Sat, 04 Feb 2006 03:22:32 GMT


    Now you can also get your assert messages for failures to log by adding this to the end of your helper.

    module Test
      module Unit
        module Assertions
          alias_method :assert_block_original, :assert_block
          def assert_block(message="assert_block failed.", &block)
                  assert_block_original(message, &block)
                rescue AssertionFailedError => error
                  raise error
  7. Avatar
    Jacolyte Fri, 05 Feb 2010 23:38:55 GMT

    Heed the statements of sb, for that is why you want to use Logger.