<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Lucid &#187; test</title>
	<atom:link href="http://thelucid.com/tag/test/feed/" rel="self" type="application/rss+xml" />
	<link>http://thelucid.com</link>
	<description>Lightweight ramblings</description>
	<lastBuildDate>Tue, 27 Jul 2010 17:41:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Unit testing by simplifying the problem: memoization</title>
		<link>http://thelucid.com/2008/12/18/unit-testing-by-simplifying-the-problem-memoization/</link>
		<comments>http://thelucid.com/2008/12/18/unit-testing-by-simplifying-the-problem-memoization/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 13:16:01 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=500</guid>
		<description><![CDATA[I see unit testing as a way to test each possible snippet of functionality and route the code in question can take. With Ruby being such a dynamic language and allowing shortcuts to common problems, sometimes it can seem somewhat of a mystery, how to test these snippets of functionality. Using Memoization as an example: [...]]]></description>
			<content:encoded><![CDATA[<p>I see unit testing as a way to test each possible snippet of functionality and route the code in question can take. With Ruby being such a dynamic language and allowing shortcuts to common problems, sometimes it can seem somewhat of a mystery, how to test these snippets of functionality.</p>
<p>Using Memoization as an example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> MyClass
  <span style="color:#9966CC; font-weight:bold;">def</span> lazy_initialized_value
    <span style="color:#0066ff; font-weight:bold;">@lazy_initialized_value</span> <span style="color:#006600; font-weight:bold;">||</span>= Expensive.<span style="color:#9900CC;">request</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>There are actually 3 separate snippets of functionality that need testing here, however it is not immediately obvious from the example. Lets be slightly more verbose about what is actually happening:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> MyClass
  <span style="color:#9966CC; font-weight:bold;">def</span> lazy_initialized_value
    <span style="color:#0066ff; font-weight:bold;">@lazy_initialized_value</span> = Expensive.<span style="color:#9900CC;">request</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0066ff; font-weight:bold;">@lazy_initialized_value</span>
    <span style="color:#0066ff; font-weight:bold;">@lazy_initialized_value</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now it is much easier to see the 3 steps the code should take:</p>
<p>* Store result of expensive request in instance variable<br />
* Leave instance variable alone when it is already set<br />
* Return the value of the instance variable</p>
<p>Now we have this information, our tests become (using Mocha to mock external methods):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Expensive; <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> <span style="color:#6666ff; font-weight:bold;">Tests::MyClass</span>
  <span style="color:#008000; font-style:italic;"># lazy_initialized_value</span>
  <span style="color:#008000; font-style:italic;"># ----------------------</span>
  <span style="color:#9966CC; font-weight:bold;">class</span> LazyInitializedValueTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span>
    <span style="color:#9966CC; font-weight:bold;">def</span> test_should_respond
      assert_respond_to MyClass.<span style="color:#9900CC;">new</span>, <span style="color:#ff3333; font-weight:bold;">:lazy_initialized_value</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> test_should_store_result_of_expensive_request_in_instance_variable
      instance = MyClass.<span style="color:#9900CC;">new</span>
      Expensive.<span style="color:#9900CC;">stubs</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:request</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">with</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">returns</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'expensive value'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      instance.<span style="color:#9900CC;">lazy_initialized_value</span>
      assert_equal <span style="color:#996600;">'expensive value'</span>, instance.<span style="color:#9900CC;">instance_variable_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'@lazy_initialized_value'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> test_should_return_value_of_instance_varable
      instance = MyClass.<span style="color:#9900CC;">new</span>
      instance.<span style="color:#9900CC;">instance_variable_set</span> <span style="color:#996600;">'@lazy_initialized_value'</span>, <span style="color:#996600;">'the value'</span>
      Expensive.<span style="color:#9900CC;">stubs</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:request</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      assert_equal <span style="color:#996600;">'the value'</span>, instance.<span style="color:#9900CC;">lazy_initialized_value</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> test_should_maintain_existing_instance_variable_value_when_already_set
      instance = MyClass.<span style="color:#9900CC;">new</span>
      instance.<span style="color:#9900CC;">instance_variable_set</span> <span style="color:#996600;">'@lazy_initialized_value'</span>, <span style="color:#996600;">'existing value'</span>
      Expensive.<span style="color:#9900CC;">stubs</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:request</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      instance.<span style="color:#9900CC;">lazy_initialized_value</span>
      assert_equal <span style="color:#996600;">'existing value'</span>, instance.<span style="color:#9900CC;">instance_variable_get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'@lazy_initialized_value'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now we have these tests in place, we can go back and refractor the code &#8217;til our heart&#8217;s content using all the tricks in the book but by simplifying the problem in the first place, it gives us a solid test suite and the confidence to make changes without breaking functionality.</p>
<p>If you were solving this problem test-first then you wouldn&#8217;t (but more likely, shouldn&#8217;t) have written the first example until re-factoring stage anyway, however when these shortcuts become engrained in your brain, it&#8217;s all too easy to forget what they are _actually_ doing.</p>
<p>So there we go, simplify the initial implementation, get a solid test suite in order, _then_ re-factor.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2008/12/18/unit-testing-by-simplifying-the-problem-memoization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails: Using Autotest with UnitRecord</title>
		<link>http://thelucid.com/2007/09/05/rails-using-autotest-with-unitrecord/</link>
		<comments>http://thelucid.com/2007/09/05/rails-using-autotest-with-unitrecord/#comments</comments>
		<pubDate>Thu, 06 Sep 2007 00:17:00 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[autotest]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[unit_record]]></category>

		<guid isPermaLink="false">http://d3754554-5787-434b-8741-474c805c4a4e</guid>
		<description><![CDATA[Myself and a colleague have just managed to waste away a good couple of hours trying to figure out Autotests strange &#8216;style&#8217; mechanism to add the ability to &#8220;test in the way Jay Fields explains&#8221;:http://blog.jayfields.com/2007/09/rails-how-we-test.html using &#8220;UnitRecord&#8221;:http://unit-test-ar.rubyforge.org/. You can grab our plugin to enable &#8220;UnitRecord&#8221;:http://unit-test-ar.rubyforge.org/ when using &#8220;Autotest&#8221;:http://rubyforge.org/projects/zentest below: &#8220;http://svn.soniciq.com/public/rails/plugins/iq_autotest&#8221;:http://svn.soniciq.com/public/rails/plugins/iq_autotest By default, running autotest in [...]]]></description>
			<content:encoded><![CDATA[<p>Myself and a colleague have just managed to waste away a good couple of hours trying to figure out Autotests strange &#8216;style&#8217; mechanism to add the ability to &#8220;test in the way Jay Fields explains&#8221;:http://blog.jayfields.com/2007/09/rails-how-we-test.html using &#8220;UnitRecord&#8221;:http://unit-test-ar.rubyforge.org/.</p>
<p>You can grab our plugin to enable &#8220;UnitRecord&#8221;:http://unit-test-ar.rubyforge.org/ when using &#8220;Autotest&#8221;:http://rubyforge.org/projects/zentest below:</p>
<p>&#8220;http://svn.soniciq.com/public/rails/plugins/iq_autotest&#8221;:http://svn.soniciq.com/public/rails/plugins/iq_autotest</p>
<p>By default, running <code>autotest</code> in the Rails directory will run the unit tests. To run the functional tests, do: <code>AUTOTEST='functional' autotest</code></p>
<p>I hope this saves some people some time!!</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2007/09/05/rails-using-autotest-with-unitrecord/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
