<?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>Live &#38; Code &#187; testing</title>
	<atom:link href="http://www.liveandcode.com/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.liveandcode.com</link>
	<description>Enrico on programming, living, and everything in between</description>
	<lastBuildDate>Mon, 27 Jun 2011 01:10:34 +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>RSpec Matchers: More Than Just Assertions</title>
		<link>http://www.liveandcode.com/2010/11/10/rspec-matchers-more-than-just-assertions/</link>
		<comments>http://www.liveandcode.com/2010/11/10/rspec-matchers-more-than-just-assertions/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 03:51:34 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=513</guid>
		<description><![CDATA[I recently read a post from Carbon Five about RSpec best practices. The most delightful thing about it was reading it after I&#8217;d been writing a spec at work and noticing that how I was doing it was close to what was being described. It was a little bit of validation, a pat on the [...]]]></description>
			<content:encoded><![CDATA[<p>I recently read a <a title="RSpec best practices - Carbon Five Community" href="http://blog.carbonfive.com/2010/10/testing/rspec-best-practices">post</a> from Carbon Five about RSpec best practices. The most delightful thing about it was reading it after I&#8217;d been writing a spec at work and noticing that how I was doing it was close to what was being described. It was a little bit of validation, a pat on the back for all of the reading, practicing, and thinking about BDD that I&#8217;d done to that point. But then Carbon Five asks &#8220;so what else?&#8221;<span id="more-513"></span></p>
<p>I recalled some of my first experiences with RSpec. For one job interview with a company that was using Rails, I was asked to bring in a small Rails app, a toy problem that they previously set for me to test my skills. I worked really hard on that application, struggling with every feature because at the time I had done relatively little with Ruby and Rails. I was specifically asked to make sure that this app was well tested. I had been doing some reading about TDD around that time, which led me to BDD, and in the context of Ruby that led me to <a title="RSpec.info: Home" href="http://rspec.info/">RSpec</a>. So I used RSpec and <a title="Spec::Rails" href="http://rspec.info/rails/">Spec::Rails</a>, though I wasn&#8217;t entirely familiar with them.</p>
<p>During that interview, the developers sitting in asked me to add a new feature to my app. I remember that I needed to check that a particular value went up by 1 when I called a method. So I stored the old value in a local variable, invoked the method, and then checked that the new value was what I expected it to be.</p>
<p>&#8220;That&#8217;s good, but it can be better.&#8221;</p>
<p>From there, I was walked through something that at the time I couldn&#8217;t fully comprehend. I was asked to create a lambda with a block invoking the method, append <code>.should</code> to that, a space, and then <code>change</code>, with another block that would produce the value I wanted to check, and then <code>.by(1)</code>. The three-line example I&#8217;d originally written became a single (albeit long and, for me at the time, hard to read) line. I was assured that it did precisely the same thing and they asked me to run it. Sure enough, it passed. That was my introduction to a whole side of RSpec I&#8217;d never learned about before: <em>matchers</em>.</p>
<p>I always wrote <code>should ==</code> or <code>should_not ==</code>. I just thought of it as the particular way that RSpec did assertions, just a small part of the entire DSL. But he&#8217;s much more powerful than he actually looks. When you call <code>should</code> on something you pass an argument. That argument is a matcher. But what is a matcher? A matcher takes an object, evaluates some sort of predicate on it, and says &#8220;yes, that matches&#8221; or &#8220;no, it doesn&#8217;t&#8221;. It can also define a friendly message for failure (that&#8217;s what you see when an expectation fails while running your specs) and a description of what that matcher asserts, which appears in the documentation output format (&#8220;specdoc&#8221; if you&#8217;re still using RSpec 1.x) if you called <code>#it</code> with just a block and no string.</p>
<p>So let&#8217;s look at something similar to what I was walked through during that interview:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">describe Foo <span style="color:#9966CC; font-weight:bold;">do</span>
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@foo</span> = Foo.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  describe <span style="color:#996600;">&quot;.harbl&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    it <span style="color:#996600;">&quot;should raise 'bar' by 1&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0066ff; font-weight:bold;">@foo</span>.<span style="color:#9900CC;">harbl</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">should</span> change <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0066ff; font-weight:bold;">@foo</span>.<span style="color:#9900CC;">bar</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">by</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</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>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, say we&#8217;ve implemented everything except the part where calling <code>#harbl</code> on a <code>Foo</code> increases the value by 1. When we run this, we&#8217;ll see a message saying something to the effect of &#8220;{ @foo.bar } should have been changed by 1, but was changed by 0.&#8221; Here, we see the first benefit using a special matcher instead of a raw assertion: the failure message actually reflects the behaviour we were expecting to see! Instead of seeing &#8220;1 doesn&#8217;t equal 2&#8243; which tells us little about what we were actually testing, we see &#8220;@foo.bar should have changed, but it didn&#8217;t.&#8221;</p>
<p>So how does this actually work? We&#8217;ll take this from the inside and work our way out. The &#8220;magic&#8221; here is <code>change</code>. It&#8217;s a method, but what does it return? It returns a fresh instance of <code><a href="https://github.com/dchelimsky/rspec/blob/master/lib/spec/matchers/change.rb">Spec::Matchers::Change</a></code> (in fact, the definition of the method is right in that file!). The block that you pass it tells the matcher what it&#8217;s supposed to evaluate to get the value before and after. We call <code>#by</code> on that which is just a little decorator. It tells the matcher what kind of change it&#8217;s looking for. We&#8217;re telling the matcher &#8220;run that block I gave you before and after running the code I plan to give you later. The value you get from that block after running the code should be 1 greater than it was when you ran it before the code.&#8221;</p>
<p>So now the matcher knows how to get the value, and it knows what we expect it to change by. It needs one last ingredient: a subject. What piece of code does the matcher need to run so it can observe the change in the value? That&#8217;s where <code>#should</code> comes in. He&#8217;s pretty simple: call him on something and he&#8217;ll take that something, pass it over to the matcher that he&#8217;s given as an argument, and say &#8220;tell me if this matches or not.&#8221; And just like that we&#8217;ve turned the matcher that we&#8217;ve built up into an expectation on the behaviour of our code.</p>
<p>That&#8217;s pretty nifty all on it&#8217;s own, but here&#8217;s where it becomes truly beautiful: RSpec comes with matchers for <a href="https://github.com/dchelimsky/rspec/tree/master/lib/spec/matchers">just about every common predicate you can think of</a>. Not only does it come with all of these matchers, but it comes with methods that dynamically generate matchers! Wait, what?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">describe Orange <span style="color:#9966CC; font-weight:bold;">do</span>
  before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@orange</span> = Orange.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  it <span style="color:#996600;">&quot;should be tangy&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#0066ff; font-weight:bold;">@orange</span>.<span style="color:#9900CC;">should</span> be_tangy
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>While the guys who made RSpec included a lot of matchers, I&#8217;m pretty sure a &#8220;be tangy&#8221; matcher is not one of them. And yet this is a perfectly valid RSpec example, without any special extensions or monkey patching. How? It turns out that RSpec&#8217;s &#8220;be&#8221; matcher is a lot smarter than it looks. If you connect it to something with an underscore, it will send that something with a <code>?</code> on the end as a message to the subject and determine pass or fail based on the response.</p>
<p>Here&#8217;s the code we would write to make that spec pass:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Orange
  <span style="color:#9966CC; font-weight:bold;">def</span> tangy?
    <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>And there you have it. We have a tangy orange, and RSpec lets us specify that it should be tangy. There is so much of this kind of awesomeness in RSpec that I can&#8217;t fit it all into this post. You really should explore RSpec matchers for yourself. If you somehow find a predicate that RSpec cannot express with one of its matchers, you can easily create your own. Just follow the model of RSpec&#8217;s other matcher classes; define a few necessary methods and some decorators if you need them and you&#8217;re done. If you&#8217;re using RSpec 2, I hear they&#8217;ve even created a small DSL to make custom matchers even easier to generate. Spiffy!</p>
<p>So if I could give only one piece of advice about RSpec best practices, it would be this: if your expectation looks messy, RSpec probably has a matcher that can make it cleaner.</p>
<p><strong><em>Use the matchers, Luke.</em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2010/11/10/rspec-matchers-more-than-just-assertions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Autotest for Mac OS X &#8211; Now with less suck!</title>
		<link>http://www.liveandcode.com/2009/05/29/autotest-for-mac-os-x-now-with-less-suck/</link>
		<comments>http://www.liveandcode.com/2009/05/29/autotest-for-mac-os-x-now-with-less-suck/#comments</comments>
		<pubDate>Fri, 29 May 2009 18:41:02 +0000</pubDate>
		<dc:creator>Enrico</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[software tools]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.liveandcode.com/?p=48</guid>
		<description><![CDATA[The autotest-fsevent and autotest-growl gems bring considerable improvements to ZenTest&#8217;s autotest for Mac OS X users. autotest-fsevent teaches autotest a new trick: using FSEvent (provided in Mac OS X 10.5.x) instead of ordinary filesystem polling.  This means less CPU usage because FSEvent broadcasts filesystem changes, making active and periodic polling unnecessary. autotest-growl enhances the Growl support that [...]]]></description>
			<content:encoded><![CDATA[<p>The <a title="Bitcetera: Mac-friendly Autotest" href="http://www.bitcetera.com/en/techblog/2009/05/27/mac-friendly-autotest/"><code>autotest-fsevent</code> and <code>autotest-growl</code></a> gems bring considerable improvements to <a href="http://www.zenspider.com/ZSS/Products/ZenTest/">ZenTest&#8217;</a>s <code>autotest</code> for Mac OS X users.</p>
<p><code>autotest-fsevent</code> teaches <code>autotest</code> a new trick: using <code>FSEvent</code> (provided in Mac OS X 10.5.x) instead of ordinary filesystem polling.  This means less CPU usage because FSEvent broadcasts filesystem changes, making active and periodic polling unnecessary.</p>
<p><code>autotest-growl</code> enhances the Growl support that <code>autotest</code> comes with by adding support for Growling results for tests (using Test::Unit), specs (using RSpec), and features (using Cucumber) and adding pretty Ruby logos to the notifications.</p>
<p>If you use Mac OS X and <code>autotest</code>, I highly recommend that you try these gems out today:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> autotest-fsevent
<span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> autotest-growl</pre></div></div>

<p>One caveat: if you&#8217;re using ZenTest 4.0.0 or older, you need to do a bit of trickery to get <code>autotest-growl</code> to work properly.  These versions of ZenTest come with their own <code>autotest</code> Growl plugin, so you need to make sure you&#8217;re requiring the Growl support from <code>autotest-growl</code> instead of ZenTest&#8217;s own.  Here&#8217;s what I ended up writing in my <code>~/.autotest</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'/Library/Ruby/Gems/1.8/gems/autotest-growl-0.1.0/lib/autotest/growl.rb'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.liveandcode.com/2009/05/29/autotest-for-mac-os-x-now-with-less-suck/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

