<?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>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>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>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>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>

