<?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; Ruby</title>
	<atom:link href="http://thelucid.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://thelucid.com</link>
	<description>The Lightweight Ramblings of Jamie Hill</description>
	<lastBuildDate>Thu, 26 Jan 2012 13:52:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Loving MongoDB but Missing Transactions</title>
		<link>http://thelucid.com/2012/01/26/loving-mongodb-but-missing-transactions/</link>
		<comments>http://thelucid.com/2012/01/26/loving-mongodb-but-missing-transactions/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 13:52:28 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[mongoid]]></category>
		<category><![CDATA[schema]]></category>
		<category><![CDATA[transactions]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=783</guid>
		<description><![CDATA[I&#8217;ve been venturing into the world of MongoDB via Mongoid in a Rails app. On one hand it&#8217;s a breath of fresh air (no migrations, flexible schemas etc.) but on the other hand I really, really miss transactions. If somebody asked me if I used transactions much in MySQL, I would probably have said no&#8230; [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been venturing into the world of <a href="http://www.mongodb.org">MongoDB</a> via <a href="http://mongoid.org/">Mongoid</a> in a Rails app. On one hand it&#8217;s a breath of fresh air (no migrations, flexible schemas etc.) but on the other hand I really, really miss transactions.</p>
<p>If somebody asked me if I used transactions much in MySQL, I would probably have said no&#8230; however, now that I don&#8217;t have them I realise just how much I used them. Things that just work when using <a href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html">ActiveRecord</a> such as creating a record and ensuring that an associated record gets created is sooo much harder.</p>
<p>MongoDB has &#8220;<a href="http://www.mongodb.org/display/DOCS/Atomic+Operations">Atomic Operations</a>&#8221; which means if you have an embedded relationship, all is good and the database will be rolled back if the entire document does not save. As soon as you have two top level collections and need to ensure that a record is created in both, things get a little hairy.</p>
<p>There seems to be a great deal of &#8220;if you need transactions just use a RDBMS&#8221; talk but what if you need a flexible schema and transactions, the last thing you want to manage is some kind of <a href="http://www.magentocommerce.com/wiki/2_-_magento_concepts_and_architecture/magento_database_diagram">EAV system like Magento</a>.</p>
<p>I&#8217;ve created <a href="http://groups.google.com/group/mongodb-user/browse_thread/thread/10da4c2571142671">a thread on the Mongo User forum</a> with a typical example so feel free to join in the discussion. I&#8217;d love to hear experienced Mongo user&#8217;s views on how to overcome these kinds of situations and what kind of applications people are running with MongoDB.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2012/01/26/loving-mongodb-but-missing-transactions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cucumber&#8217;s &#8220;tableish&#8221; depreciated</title>
		<link>http://thelucid.com/2011/11/22/cucumbers-tableish-depreciated/</link>
		<comments>http://thelucid.com/2011/11/22/cucumbers-tableish-depreciated/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 16:14:41 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[capybara]]></category>
		<category><![CDATA[tableish]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=761</guid>
		<description><![CDATA[I was just running some cucumber features on an old project and came across a problem whereby &#8220;tableish&#8221; has been depreciated in Cucumber. Dennis Reimann has posted a nice alternative here using Capybara&#8230; thanks Dennis.]]></description>
			<content:encoded><![CDATA[<p>I was just running some cucumber features on an old project and came across a problem whereby <a href="https://github.com/cucumber/cucumber-rails/issues/145">&#8220;tableish&#8221; has been depreciated in Cucumber</a>.</p>
<p>Dennis Reimann has posted a <a href="http://dennisreimann.de/blog/capybara-finder-for-cucumber-rails-deprecated-tableish/">nice alternative here using Capybara</a>&#8230; thanks Dennis.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2011/11/22/cucumbers-tableish-depreciated/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Releasing IQ::Color</title>
		<link>http://thelucid.com/2011/01/18/releasing-iqcolor/</link>
		<comments>http://thelucid.com/2011/01/18/releasing-iqcolor/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 03:35:49 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SonicIQ]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[gem]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=728</guid>
		<description><![CDATA[At SonicIQ we have a number of RubyGems used internally that we haven&#8217;t found time to open-source yet i.e. cleanup the documentation and get into state that people can find useful. I&#8217;m going to make it my mission to get some of these out there which first involves releasing some of the smaller Gems that [...]]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://soniciq.com">SonicIQ</a> we have a number of RubyGems used internally that we haven&#8217;t found time to open-source yet i.e. cleanup the documentation and get into state that people can find useful. I&#8217;m going to make it my mission to get some of these out there which first involves releasing some of the smaller Gems that the larger ones depend on.</p>
<p><a href="https://github.com/soniciq/iq-color">IQ::Color</a> is a really simple Gem for converting colour values to and from CSS(3) notation, this is used by a couple of our larger Gems but I can see it being of use on it&#8217;s own. A simple example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">color1 = <span style="color:#6666ff; font-weight:bold;">IQ::Color</span>.<span style="color:#9900CC;">RGB</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">127</span>, <span style="color:#006666;">127</span>, <span style="color:#006666;">255</span><span style="color:#006600; font-weight:bold;">&#41;</span>
color1.<span style="color:#9900CC;">to_css</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;#7f7fff&quot;</span>
&nbsp;
color2 = <span style="color:#6666ff; font-weight:bold;">IQ::Color</span>.<span style="color:#9900CC;">from_css</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'rgba(127,127,255,0.4)'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
color2.<span style="color:#9900CC;">red</span>    <span style="color:#008000; font-style:italic;">#=&gt; 127</span>
color2.<span style="color:#9900CC;">green</span>  <span style="color:#008000; font-style:italic;">#=&gt; 127</span>
color2.<span style="color:#9900CC;">blue</span>   <span style="color:#008000; font-style:italic;">#=&gt; 255</span>
color2.<span style="color:#9900CC;">alpha</span>  <span style="color:#008000; font-style:italic;">#=&gt; 0.4</span>
color2.<span style="color:#9900CC;">to_css</span> <span style="color:#008000; font-style:italic;">#=&gt; &quot;rgba(127,127,255,0.4)&quot;</span></pre></div></div>

<p>More examples can be found on <a href="https://github.com/soniciq/iq-color">GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2011/01/18/releasing-iqcolor/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Causing MacBook Pro to Run Hot</title>
		<link>http://thelucid.com/2011/01/06/ruby-causing-macbook-pro-to-run-hot/</link>
		<comments>http://thelucid.com/2011/01/06/ruby-causing-macbook-pro-to-run-hot/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 08:24:14 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Mac / OS X]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[fan]]></category>
		<category><![CDATA[hot]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[osx]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=704</guid>
		<description><![CDATA[The past couple of days the fan on my MacBook Pro has been constantly on and battery usage down to around a third. Launching Activity Monitor, it showed 3 Ruby processes all at 100% CPU usage. The solution was to force quit these processes and within seconds, the fan slowed up and the battery indicator [...]]]></description>
			<content:encoded><![CDATA[<p>The past couple of days the fan on my MacBook Pro has been constantly on and battery usage down to around a third. Launching Activity Monitor, it showed 3 Ruby processes all at 100% CPU usage. The solution was to force quit these processes and within seconds, the fan slowed up and the battery indicator went up. Force quitting these processes didn&#8217;t affect anything I was doing with Rails or IRB so I guess they were just stray processes.</p>
<p>If anyone else gets the same problem, just launch Activity Monitor which lives in the /Applications/Utilities folder, click the CPU tab if not already selected, select processes with a process name of &#8220;ruby&#8221;, click &#8220;Quit Process&#8221; and then select &#8220;Force Quit&#8221;. Just selecting &#8220;Quit&#8221; wouldn&#8217;t work for me hence the &#8220;Force Quit&#8221;.</p>
<p>I hope this saves someone else the head-scratching as to why their Mac is running hot.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2011/01/06/ruby-causing-macbook-pro-to-run-hot/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using Artifice to Stub Server Responses</title>
		<link>http://thelucid.com/2011/01/04/using-artifice-to-stub-server-responses/</link>
		<comments>http://thelucid.com/2011/01/04/using-artifice-to-stub-server-responses/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 19:29:59 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[artifice]]></category>
		<category><![CDATA[mocking]]></category>
		<category><![CDATA[stubbing]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=700</guid>
		<description><![CDATA[In a recent project I needed a way to fake a response from a server in my Cucumber features. Specifically, I was testing integration with a payment gateway and wanted to stub it&#8217;s responses based on different requests. In the past I have used FakeWeb, however it becomes a little hairy when you need to [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent project I needed a way to fake a response from a server in my Cucumber features. Specifically, I was testing integration with a payment gateway and wanted to stub it&#8217;s responses based on different requests.</p>
<p>In the past I have used <a href="http://fakeweb.rubyforge.org/">FakeWeb</a>, however it becomes a little hairy when you need to stub a response based on request body. I came across a couple of alternatives, firstly <a href="https://github.com/bblimke/webmock">WebMock</a> which looks promising but then <a href="https://github.com/wycats/artifice">Artifice</a> from the mighty <a href="http://yehudakatz.com/">Yehuda Katz</a> caught my eye&#8230;</p>
<p>Artifice lets you &#8220;replace the Net::HTTP subsystem of Ruby with an equivalent that routes all requests to a Rack application&#8221;. I like the simplicity of this solution as you can in essence use a <a href="https://github.com/rack/rack">Rack</a> application to replace the responses of the service you are testing.</p>
<h3>An Example</h3>
<p>First we create a simple Rack application to stand in for the server we are interacting with.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">app = <span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>env<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">200</span>, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;Content-Type&quot;</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;text/html&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>,
    <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;Hello world: #{env.inspect}&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then we simply use Artifice&#8217;s &#8220;activate_with&#8221; method to wrap any requests.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Artifice.<span style="color:#9900CC;">activate_with</span><span style="color:#006600; font-weight:bold;">&#40;</span>app<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  response = <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">start</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;google.com&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>http<span style="color:#006600; font-weight:bold;">|</span>
    http.<span style="color:#9900CC;">post</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/the_url&quot;</span>, <span style="color:#996600;">&quot;foo=bar&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> response.<span style="color:#9900CC;">body</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This allows for a Rack app to be used as a stand in for a complete API, it could be a Sinatra app for example allowing for easy route handling. We could go so far as to have a series of Rack apps that can be used as stand-ins for common API&#8217;s.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2011/01/04/using-artifice-to-stub-server-responses/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why I stopped using Pickle with Cucumber</title>
		<link>http://thelucid.com/2010/02/07/why-i-stopped-using-pickle-with-cucumber/</link>
		<comments>http://thelucid.com/2010/02/07/why-i-stopped-using-pickle-with-cucumber/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 02:12:48 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Cucumber]]></category>
		<category><![CDATA[pickle]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=558</guid>
		<description><![CDATA[&#8230;no, not because it leaves a bitter aftertaste, I&#8217;m talking about the Pickle step definitions for Cucumber. I have lately been using Pickle when writing Cucumber features, however I have come to the conclusion that this is a bad idea. The reason being that when using Pickle, you create entries directly, whereas the whole point [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230;no, not because it leaves a bitter aftertaste, I&#8217;m talking about the <a href="http://github.com/ianwhite/pickle">Pickle</a> step definitions for <a href="http://github.com/aslakhellesoy/cucumber/">Cucumber</a>.</p>
<p>I have lately been using <a href="http://github.com/ianwhite/pickle">Pickle</a> when writing <a href="http://github.com/aslakhellesoy/cucumber/">Cucumber</a> features, however I have come to the conclusion that this is a bad idea. The reason being that when using Pickle, you create entries directly, whereas the whole point of Cucumber is that it is for high level integration testing.</p>
<p>What I do now, is to create any entries by filling out and submitting the relevant forms with a step definition, for example I may have the following:</p>
<pre>
Given an admin has created the following products
  | Name    | Variants                                  | Featured |
  | T-Shirt | Small: 10.99, Medium: 12.99, Large: 14.99 | Yes      |
  | Keyring | Default: 2.99                             | Yes      |
</pre>
<p>I would then write a step definition for this that would log in as the admin user, break this table appart and fill in the relevant forms.</p>
<p>I would go so far as to say that using factories at all in features is a bad idea and instead everything should happen via the user interface for better coverage. For any data that is known to exist when the app is deployed via &#8216;rake db:seed&#8217;, this can be loaded in the &#8216;env.rb&#8217; file e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">load</span> <span style="color:#996600;">'seeds.rb'</span></pre></div></div>

<p><strong>Update 11/2/2010:</strong> Sometimes this is simply not practical due to slowdown which is a shame, as noted by Amos in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2010/02/07/why-i-stopped-using-pickle-with-cucumber/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Devise Rails Authentication Gem Rocks!</title>
		<link>http://thelucid.com/2010/02/06/devise-rails-authentication-gem-rocks/</link>
		<comments>http://thelucid.com/2010/02/06/devise-rails-authentication-gem-rocks/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 21:36:28 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[authentication]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=555</guid>
		<description><![CDATA[I&#8217;ve had a set of rather bespoke requirements for authentication on a recent project and thought I&#8217;d give Devise a go. Devise uses Warden, which is an authentication solution build on Rack making it extremely flexible and usable across multiple frameworks e.g. Rails/Sinatra. Also, Devise is extremely modular meaning to can easily write custom &#8220;strategies&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had a set of rather bespoke requirements for authentication on a recent project and thought I&#8217;d give <a href="http://github.com/plataformatec/devise">Devise</a> a go.</p>
<p>Devise uses <a href="http://github.com/hassox/warden/">Warden</a>, which is an authentication solution build on <a href="http://github.com/rack/rack">Rack</a> making it extremely flexible and usable across multiple frameworks e.g. Rails/Sinatra. Also, Devise is extremely modular meaning to can easily write custom &#8220;strategies&#8221; for specific behaviour.</p>
<p>I have used <a href="http://github.com/thoughtbot/clearance/">Clearance</a> in the past which is great if you want an engine that will just work. Devise however is by far the most flexible and extensible solution that I have come across with the same ease of use as Clearance. The only thing that you don&#8217;t get with devise that you do with Clearance is the signup stage, however as this is normally custom on a per-app basis I can live with this.</p>
<p>One more thing to note is that Devise lets you have multiple auth systems in play e.g. one for users and one for admins.</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2010/02/06/devise-rails-authentication-gem-rocks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">Test::Unit::TestCase</span></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>Stubbing case statements with Mocha</title>
		<link>http://thelucid.com/2008/12/04/stubbing-case-statements-with-mocha/</link>
		<comments>http://thelucid.com/2008/12/04/stubbing-case-statements-with-mocha/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 23:04:22 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[mocha]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unit]]></category>

		<guid isPermaLink="false">http://thelucid.com/?p=498</guid>
		<description><![CDATA[I was happily mocking away with Mocha, then I passed a stub to a case statement at which point I was a little flummoxed. The Ruby documentation clearly states that the &#8216;when&#8217; in a &#8216;case&#8217; statement uses the &#8216;===&#8217; method for comparing the subject so I couldn&#8217;t work out why something like the following wasn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>I was happily mocking away with <a href="http://mocha.rubyforge.org/">Mocha</a>, then I passed a stub to a case statement at which point I was a little flummoxed.</p>
<p>The Ruby documentation clearly states that the &#8216;when&#8217; in a &#8216;case&#8217; statement uses the &#8216;===&#8217; method for comparing the subject so I couldn&#8217;t work out why something like the following wasn&#8217;t working:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Code</span>
<span style="color:#9966CC; font-weight:bold;">class</span> A; <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> foo<span style="color:#006600; font-weight:bold;">&#40;</span>instance<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> instance
    <span style="color:#9966CC; font-weight:bold;">when</span> A : <span style="color:#996600;">'an A instance'</span>
    <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#996600;">'not an A instance'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Test</span>
a = stub_everything
a.<span style="color:#9900CC;">stubs</span><span style="color:#006600; font-weight:bold;">&#40;</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>A<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:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
assert_equal <span style="color:#996600;">'an A instance'</span>, foo<span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>So I had a little play around in irb and found the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> A; <span style="color:#9966CC; font-weight:bold;">end</span>
a = A.<span style="color:#9900CC;">new</span>
a === A <span style="color:#008000; font-style:italic;">#=&gt; false</span>
A === a <span style="color:#008000; font-style:italic;">#=&gt; true</span></pre></div></div>

<p>This revealed that I was actually stubbing the wrong side of the operator, I changed this to the following and voila!</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Code</span>
<span style="color:#9966CC; font-weight:bold;">class</span> A; <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> foo<span style="color:#006600; font-weight:bold;">&#40;</span>instance<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> instance
    <span style="color:#9966CC; font-weight:bold;">when</span> A : <span style="color:#996600;">'an A instance'</span>
    <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#996600;">'not an A instance'</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Test</span>
a = stub_everything
A.<span style="color:#9900CC;">stubs</span><span style="color:#006600; font-weight:bold;">&#40;</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>a<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:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span>
assert_equal <span style="color:#996600;">'an A instance'</span>, foo<span style="color:#006600; font-weight:bold;">&#40;</span>a<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Looks obvious now but certainly wasn&#8217;t at the time!</p>
]]></content:encoded>
			<wfw:commentRss>http://thelucid.com/2008/12/04/stubbing-case-statements-with-mocha/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>

