<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Robby on Rails: Rails, Logger, and those pesky Tests</title>
    <link>http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>thoughts.sort_by{|t| t[:topic]}.collect </description>
    <item>
      <title>Rails, Logger, and those pesky Tests</title>
      <description>&lt;p&gt;First of all, I would like to thank all of you who took a moment to &lt;a href="http://www.robbyonrails.com/articles/2006/01/09/request-for-rails-debugging-tools"&gt;gather around the campfire with me&lt;/a&gt; and share &lt;a href="http://www.robbyonrails.com/articles/2006/01/09/request-for-rails-debugging-tools#comments"&gt;your stuff&lt;/a&gt;. That was much appreciated.&lt;/p&gt;


	&lt;p&gt;&lt;img src="http://www.planetargon.com/files/~robby/cowboy_kids.jpg" style="float: right;" &gt;&lt;/p&gt;


	&lt;p&gt;So, I started playing with the idea of logging in unit and functional tests and I quickly realized that calling &lt;code&gt;logger.info&lt;/code&gt; wasn&amp;#8217;t an option. What&amp;#8217;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.&lt;/p&gt;


You&amp;#8217;re all in luck because:
	&lt;ul&gt;
	&lt;li&gt;A) my holster was eaten by my dog
and&lt;/li&gt;
		&lt;li&gt;B) I found a quick solution to clean up this &lt;code&gt;logger&lt;/code&gt; situation.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;Okay&amp;#8230; are you ready?&lt;/p&gt;


	&lt;p&gt;I know&amp;#8230; this is &lt;strong&gt;totally groundbreaking&lt;/strong&gt;!!!&lt;/p&gt;


	&lt;p&gt;Open up &lt;code&gt;test/test_helper.rb&lt;/code&gt; and add the method&amp;#8230; &lt;code&gt;logger&lt;/code&gt;.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;# other stuff at the top of the file... just &lt;/span&gt;
&lt;span class="comment"&gt;# look down below at def logger&lt;/span&gt;
&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Test::Unit::TestCase&lt;/span&gt;
  &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;use_transactional_fixtures&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;true&lt;/span&gt;
  &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;use_instantiated_fixtures&lt;/span&gt;  &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;false&lt;/span&gt;

  &lt;span class="comment"&gt;# here... look here! right below this&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;logger&lt;/span&gt;
    &lt;span class="constant"&gt;RAILS_DEFAULT_LOGGER&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Save that and pick up your pistol&amp;#8230;&lt;/p&gt;


	&lt;p&gt;The next step is to call the logger method in your unit and functional tests.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;test_the_obvious&lt;/span&gt;
  &lt;span class="ident"&gt;logger&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;info&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;asserting that 1 is 1&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;)&lt;/span&gt;
  &lt;span class="ident"&gt;assert&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="number"&gt;1&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;I &lt;a href="http://rails.techno-weenie.net/tip/2005/11/20/log_within_tests"&gt;posted this&lt;/a&gt; on &lt;a href="http://rails.techno-weenie.net"&gt;Rails Weenie&lt;/a&gt; as well.&lt;/p&gt;
</description>
      <pubDate>Wed, 25 Jan 2006 16:06:00 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:3e1240ab-de42-4ef0-b436-7de4a28f1efe</guid>
      <author>Robby Russell</author>
      <link>http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests</link>
      <category>rails</category>
      <category>testing</category>
    </item>
    <item>
      <title>"Rails, Logger, and those pesky Tests" by Victor Cosby</title>
      <description>&lt;p&gt;Sweet.&lt;/p&gt;


	&lt;p&gt;Now you can also get your assert messages for failures to log by adding this to the end of your helper.&lt;/p&gt;


&lt;pre&gt;
module Test
  module Unit
    module Assertions
      alias_method :assert_block_original, :assert_block

      def assert_block(message="assert_block failed.", &amp;#38;block)
            begin
              assert_block_original(message, &amp;#38;block)
            rescue AssertionFailedError =&amp;gt; error
              logger.info(message.to_s)
              raise error
            end
      end
    end
  end
end
&lt;/pre&gt;</description>
      <pubDate>Fri, 03 Feb 2006 21:22:32 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:32109ad8-7d5d-43ab-ac25-d3bcb11a4679</guid>
      <link>http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests#comment-1816</link>
    </item>
    <item>
      <title>"Rails, Logger, and those pesky Tests" by logger</title>
      <description>&lt;p&gt;You want logging in unit tests so you can help debug what went wrong when a problem is found. Logging is for always.&lt;/p&gt;</description>
      <pubDate>Sun, 29 Jan 2006 09:16:43 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:58e4241e-5943-4278-819e-771db9bb641b</guid>
      <link>http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests#comment-1797</link>
    </item>
    <item>
      <title>"Rails, Logger, and those pesky Tests" by Robby Russell</title>
      <description>&lt;p&gt;I find that when I&amp;#8217;m testing some more complex units of test and want to get some information about what is happening before and after each database interaction&amp;#8230; having some of my own text help break things out gives me clarity. :-)&lt;/p&gt;</description>
      <pubDate>Fri, 27 Jan 2006 10:49:54 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:756e72f1-e69f-43ba-9572-ca9186405326</guid>
      <link>http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests#comment-1794</link>
    </item>
    <item>
      <title>"Rails, Logger, and those pesky Tests" by Mitch</title>
      <description>&lt;p&gt;I am also wondering why logging would be important in tests. I use $stdout.puts statements right now if I need to know anything.&lt;/p&gt;

&lt;p&gt;But that was a smart idea ;) &lt;/p&gt;</description>
      <pubDate>Fri, 27 Jan 2006 09:13:51 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:f90e7f04-10b4-470d-91fa-fe69c75eccad</guid>
      <link>http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests#comment-1793</link>
    </item>
    <item>
      <title>"Rails, Logger, and those pesky Tests" by Damien Tanner</title>
      <description>&lt;p&gt;My god the ground is breaking beneath my feet, only yesterday was I wondering how to do this.&lt;/p&gt;</description>
      <pubDate>Thu, 26 Jan 2006 05:26:02 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:157120f2-9e29-48d4-a2a0-de07ae0b960a</guid>
      <link>http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests#comment-1792</link>
    </item>
    <item>
      <title>"Rails, Logger, and those pesky Tests" by Pat</title>
      <description>&lt;p&gt;Maybe you posted this in another blog, but I&amp;#8217;m curious as to why you&amp;#8217;d want logging in unit tests.  It&amp;#8217;s not like you&amp;#8217;re getting diagnostic info from your app in development mode that you don&amp;#8217;t want filling up your production logs..simple puts statements should do the trick in unit tests, shouldn&amp;#8217;t they?&lt;/p&gt;</description>
      <pubDate>Wed, 25 Jan 2006 16:35:22 -0600</pubDate>
      <guid isPermaLink="false">urn:uuid:001f5a6a-499d-4d08-862a-733ebd55a1a7</guid>
      <link>http://www.robbyonrails.com/articles/2006/01/25/rails-logger-and-those-pesky-tests#comment-1791</link>
    </item>
  </channel>
</rss>
