<?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>nick evans - devblog &#187; rspec</title>
	<atom:link href="http://ekenosen.net/nick/devblog/tag/rspec/feed/" rel="self" type="application/rss+xml" />
	<link>http://ekenosen.net/nick/devblog</link>
	<description>thoughts on software craftsmanship and other technobabble</description>
	<lastBuildDate>Wed, 27 May 2009 18:00:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>please don&#8217;t use before(:all)</title>
		<link>http://ekenosen.net/nick/devblog/2009/02/please-dont-use-before-all/</link>
		<comments>http://ekenosen.net/nick/devblog/2009/02/please-dont-use-before-all/#comments</comments>
		<pubDate>Sat, 07 Feb 2009 08:00:01 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[rspec]]></category>

		<guid isPermaLink="false">http://ekenosen.net/nick/devblog/?p=167</guid>
		<description><![CDATA[Please do not use &#8220;before(:all)&#8221; (in rspec) unless it is imperative for performance reasons.  Even then, it should only be used with the utmost care given towards any global or shared state, and with no expectations on order of the spec run.  You will cause strange build failures if you do not heed [...]]]></description>
			<content:encoded><![CDATA[<p>Please do not use &#8220;<code>before(:all)</code>&#8221; (in rspec) unless it is imperative for performance reasons.  Even then, it should only be used with the utmost care given towards any global or shared state, and with no expectations on order of the spec run.  You <em>will</em> cause strange build failures if you do not heed this warning.</p>
<p>Each individual example (&#8221;<code>it</code>&#8221; blocks) should be able to run by itself or in any order and the example group (&#8221;<code>describe</code>&#8221; blocks) may run in any order, even in random order.</p>
<p><em>Corollaries</em>:  If you change any global or shared state inside an example, please ensure that you reset it to a baseline before the next example.  If you depend on any global shared state, please set it to a baseline before this example.  Both of these are achieved easily with a <code>before(:each)</code> block (or <code>after(:each)</code> in some cases).</p>
<p>Nine times out of ten, when I see a weird sporadic build failure and there is a <code>before(:all)</code> nearby, I can fix it merely by changing it to a <code>before(:each)</code>.   The original author of the spec assumed that it would always run front to back and then proceed to change global or shared state in some of examples (sometimes inadvertantly).</p>
<p>Although I&#8217;ve phrased this advice for rspec, it applies equally to test/unit in ruby, or xUnit on any platform.</p>
]]></content:encoded>
			<wfw:commentRss>http://ekenosen.net/nick/devblog/2009/02/please-dont-use-before-all/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>mock abuse &#8211; asserting the implementation</title>
		<link>http://ekenosen.net/nick/devblog/2009/02/mock-abuse/</link>
		<comments>http://ekenosen.net/nick/devblog/2009/02/mock-abuse/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 17:00:10 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[shoulda]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://ekenosen.net/nick/devblog/?p=109</guid>
		<description><![CDATA[I use mocks (and stubs and other test doubles) for checking expectations: I&#8217;m a &#8220;mockist&#8220;.  Likewise, a guideline for specs or unit tests is to focus on testing your code (separately from integration tests which might exercise the full stack), and assume that the framework has tested its own code.  However, I think that this [...]]]></description>
			<content:encoded><![CDATA[<p>I use mocks (and stubs and other test doubles) for checking expectations: I&#8217;m a &#8220;<a href="http://martinfowler.com/articles/mocksArentStubs.html">mockist</a>&#8220;.  Likewise, a guideline for specs or unit tests is to focus on testing <em>your</em> code (separately from integration tests which might exercise the full stack), and assume that the framework has tested its own code.  However, I think that this guideline is sometimes taken too far, leading to mock abuse.</p>
<p>Here are some guidelines that help me follow another important principle: <em>test the interface, not the implementation</em>.</p>
<ul>
<li>If the test looks like a copy and paste or trivial transformation of the production code, something is probably wrong.</li>
<li>&#8220;Complex&#8221; code should be avoided in specs, <em>especially</em> if it is a reproduction of the production code (e.g. SQL, <code>ActiveRecord::Base#find</code> conditions, and non-trivial regular expressions).</li>
<li>I prefer business language <em>examples</em> over technical language <em>modeling</em> (e.g. &#8220;<code>belongs_to</code>&#8221; or &#8220;<code>verifies_format_of</code>&#8220;).</li>
</ul>
<h3>Some (bad) examples</h3>
<p>As an example of what I&#8217;m talking about: <a href="http://tammersaleh.com/posts/testing-named_scope">Testing Named Scope with shoulda&#8217;s <code>should_have_named_scope</code></a> (also <code>should_belong_to</code>,  <code>should_have_one</code>, and <code>should_have_many</code> from <a href="http://dev.thoughtbot.com/shoulda/classes/ThoughtBot/Shoulda/ActiveRecord/Macros.html">shoulda&#8217;s ActiveRecord Macros</a>).  <a href="http://api.rubyonrails.com/classes/ActiveRecord/NamedScope/ClassMethods.html#M001906">The rails documentation for <code>named_scope</code></a> also suggests that you can test it by looking at the <code>proxy_options</code> method.</p>
<p>What&#8217;s wrong with the following?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># from rails rdoc:</span>
expected_options = <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:colored</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'red'</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
assert_equal expected_options, Shirt.<span style="color:#9900CC;">colored</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'red'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">proxy_options</span>
<span style="color:#008000; font-style:italic;"># from shoulda</span>
should_have_named_scope <span style="color:#ff3333; font-weight:bold;">:eighteen</span>,  <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:age</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">18</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># example of the sort of thing I've seen in codebases I work with</span>
Model.<span style="color:#9900CC;">should_receive</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:find</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:#ff3333; font-weight:bold;">:all</span>,
           :conditions <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;foo =&gt; ? and (bar like ? or blatz in ?)&quot;</span>,
                           foo, bar, blatz<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span>...<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># I've also seen bastardizations of the above for SQL building</span>
<span style="color:#008000; font-style:italic;"># code which gets piped into a mocked Model.find_by_sql</span></pre></div></div>

<p>Should your specs care if those conditions are created as hashes or arrays, or even at all?  From the outside, it is completely unimportant whether it uses named_scope or a conditions hash behind the scenes.   What you <em>should</em> care about is whether or not they return the correct objects, don&#8217;t return the wrong objects, and (maybe) stack appropriately with other named scopes or finder methods.</p>
<p>More practically, those conditions hashes qualify as production code that represents the <em>very core of the business logic</em> you are supposed to be testing.  If you expect that <code>{:colored =&gt; 'red'}</code>, but the column name is &#8220;<code>color</code>&#8221; or the value is actually stored in hex RGB, you won&#8217;t have a hint that the code is broken.  If you expect that <code>{:age =&gt; 18},</code> your specs won&#8217;t remind you that you were actually supposed to be matching 18 and older.  If you expect the wrong SQL, then your specs have lost their power to help you find the right SQL.  You are <em>wide open</em> for letting sloppy bugs through the very specs that should have caught them.</p>
<p>So, these mocks verify expectations that don&#8217;t matter much, but they copy and paste the bits that <em>do</em> matter, the parts you really should be testing.   You are gaining &#8220;code coverage&#8221;, and thus false confidence.  It&#8217;s like a weird form of <a href="http://en.wikipedia.org/wiki/Regulatory_capture">regulatory capture</a>.  However, a few examples of what you should and should not match would clarify many mistakes immediately.  When working with examples, the boundaries that you need to poke often become obvious.</p>
<h4>Good examples: Named scope or finder methods</h4>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># the following lines would generally be done using factories</span>
good_examples = <span style="color:#006600; font-weight:bold;">&#91;</span>FooBar.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>...<span style="color:#006600; font-weight:bold;">&#41;</span>,
                 FooBar.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>...<span style="color:#006600; font-weight:bold;">&#41;</span>, ...<span style="color:#006600; font-weight:bold;">&#93;</span>
bad_examples  = <span style="color:#006600; font-weight:bold;">&#91;</span>FooBar.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>...<span style="color:#006600; font-weight:bold;">&#41;</span>
                 FooBar.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>...<span style="color:#006600; font-weight:bold;">&#41;</span>, ...<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#008000; font-style:italic;"># now exercise the code under test</span>
results = FooBar.<span style="color:#9900CC;">my_spectacular_finder_or_named_scope</span><span style="color:#006600; font-weight:bold;">&#40;</span>...<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#008000; font-style:italic;"># expectations</span>
good_examples.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> results.<span style="color:#9900CC;">should</span>     <span style="color:#9966CC; font-weight:bold;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
bad_examples. <span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> results.<span style="color:#9900CC;">should_not</span> <span style="color:#9966CC; font-weight:bold;">include</span><span style="color:#006600; font-weight:bold;">&#40;</span>e<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#008000; font-style:italic;"># or perhaps</span>
results.<span style="color:#9900CC;">should</span> == good_examples</pre></div></div>

<p>This wins in almost every regard over mocking SQL or checking the conditions hash or proxy_options.  It is clear what is being tested and how. We are oriented towards results, not implementation. The examples should make it clear if we are missing anything.</p>
<p>However, it is not as concise, but it doesn&#8217;t matter how concise bad specs are.  We could make macros to make the above code more concise, while still retaining the clarity.  It would also be nice to follow the &#8220;one assertion per example&#8221; rule, so that developers get fine grained failure notifications.</p>
<p>And it may be slower if it hits the DB, but it doesn&#8217;t matter how fast bad specs run.  This is a trade-off  of the ActiveRecord ORM style: to test that you wrote the right queries, you need run the queries and verify the results.</p>
<p>Alternatively, this style is well suited to <a href="http://cukes.info/">cucumber</a> or <a href="http://fitnesse.org/">fit</a> tables, enabling the subject matter experts to easily contribute their own examples (and detect mistakes that the developer wouldn&#8217;t know about).</p>
<h3>Good examples: Validations</h3>
<p>Sorry to pick on <a href="http://thoughtbot.com/projects/shoulda/">shoulda</a> above&#8230; but it was the easiest example to google for.  To balance the scales there, I do like two of the macros shoulda uses for validations: <code>should_allow_values_for</code> and <code>should_not_allow_values_for</code>.  I also wrote up a short little rspec matcher that does about the same thing.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># shoulda's macros</span>
should_not_allow_values_for <span style="color:#ff3333; font-weight:bold;">:phone_number</span>, <span style="color:#996600;">&quot;abcd&quot;</span>, <span style="color:#996600;">&quot;1234&quot;</span>
should_allow_values_for     <span style="color:#ff3333; font-weight:bold;">:phone_number</span>, <span style="color:#996600;">&quot;(123) 456-7890&quot;</span>
<span style="color:#008000; font-style:italic;"># my rspec validation matchers</span>
it <span style="color:#006600; font-weight:bold;">&#123;</span> should.<span style="color:#9900CC;">reject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:phone_number</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">of</span> <span style="color:#996600;">&quot;abcd&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
it <span style="color:#006600; font-weight:bold;">&#123;</span> should.<span style="color:#9900CC;">reject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:phone_number</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">of</span> <span style="color:#996600;">&quot;1234&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
it <span style="color:#006600; font-weight:bold;">&#123;</span> should.<span style="color:#9900CC;">accept</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:phone_number</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">of</span> <span style="color:#996600;">&quot;(123) 456-7890&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
it <span style="color:#006600; font-weight:bold;">&#123;</span> should.<span style="color:#9900CC;">accept</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:phone_number</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">of</span> <span style="color:#996600;">&quot;(123)456-7890&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
it <span style="color:#006600; font-weight:bold;">&#123;</span> should.<span style="color:#9900CC;">accept</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:phone_number</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">of</span> <span style="color:#996600;">&quot;123-456-7890&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Specs that verify that the &#8220;appropriate&#8221; regular expression was sent to <code>validates_format_of</code> scare me.  Regular expressions aren&#8217;t just opaque blobs, they are a DSL, a programming language in their own right.  Regexes are code, and we test code.  You shouldn&#8217;t care that a particular regular expression is sent to a particular rails validation macro; you care that <em>your model validations work appropriately</em>, i.e. accept some values and reject others.</p>
<h3>When I&#8217;ll ignore my own advice</h3>
<p>Here are some contraindications:</p>
<p><strong><em>I just don&#8217;t know what to do</em></strong>:  I might be exploring new realms, and feel a bit lost.  Writing some specs, <em>any</em> specs, is a good way to learn, a good way to keep focused while working, and a good way to just get the job done (<a href="http://rubyhoedown2008.confreaks.com/05-bryan-liles-lightning-talk-tatft-test-all-the-f-in-time.html">TATFT</a>).  Even if the specs are no good and tightly coupled to implementation, at least they can act as &#8220;change notifiers&#8221;.  The next time someone edits the code, they&#8217;ll trigger a build failure that will force them to re-evaluate the specs.  Maybe they&#8217;ll come at it with more knowledge and improve the situation.</p>
<p>Pair programming <em>significantly</em> reduces the likelihood of this occurring.</p>
<p><strong><em>too much yak shaving, time to punt</em></strong>:  I know what to do, but it&#8217;s too much work.  For example, if you have some models that are so truly heinous that you cannot simply or clearly create them right there in your spec, my first instinct would be to invest some time in my <a href="http://www.dcmanges.com/blog/38">factories</a>.  It&#8217;ll pay off.  But that is <a href="http://sethgodin.typepad.com/seths_blog/2005/03/dont_shave_that.html">yak-shaving</a>, and it may be too much for now.  &#8220;Emergency mocking&#8221; is excusable.  Document your compromise in the spec, and hopefully that <a href="http://martinfowler.com/bliki/TechnicalDebt.html">technical debt</a> will be repaid in the future.</p>
<p>But excessive mocking can feel like yak-shaving too.  Weigh your options before going the &#8220;quick and dirty&#8221; route; it might actually be <em>slow</em> and dirty.</p>
<p><strong><em>debugging and sanity checks</em></strong>:  If I&#8217;m trying to figure out <em>why</em> the results aren&#8217;t what I expect them to be, I&#8217;ll probably throw in some extra assertions about the implementation, as a sanity check.  I might delete them when I&#8217;m done or keep them in but document them as special.</p>
<p>There may be other contraindications, but these are what come to my mind.</p>
<p>What do you think?</p>
]]></content:encoded>
			<wfw:commentRss>http://ekenosen.net/nick/devblog/2009/02/mock-abuse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>a better progress bar for rspec</title>
		<link>http://ekenosen.net/nick/devblog/2008/12/better-progress-bar-for-rspec/</link>
		<comments>http://ekenosen.net/nick/devblog/2008/12/better-progress-bar-for-rspec/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 20:00:26 +0000</pubDate>
		<dc:creator>nick</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bdd]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://ekenosen.net/nick/devblog/?p=53</guid>
		<description><![CDATA[Updated 2008-12-10: added screenshot and video.
I&#8217;ve always been a little bit bemused by the default ruby test/unit and rspec output: a series of dots, one for each passing test (or example), and the dots become &#8220;F&#8221; characters in the event of a failure.  Most importantly: it&#8217;s simple, unambiguous about pass/fail, and easy to read at [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Updated 2008-12-10:</strong> <em>added screenshot and video.</em></p>
<p>I&#8217;ve always been a little bit bemused by the default ruby test/unit and rspec output: a series of dots, one for each passing test (or example), and the dots become &#8220;F&#8221; characters in the event of a failure.  Most importantly: it&#8217;s simple, unambiguous about pass/fail, and easy to read at a glance, so no problem there.  You get a feel for the speed at which the tests are running, and you can visualize how many tests have run already.  And it&#8217;s easy enough to have them output in color (red or green), to make the errors visually pop out.  And it&#8217;s drop dead simple.  That&#8217;s good.</p>
<h3>So what&#8217;s missing?</h3>
<p><em>(for my tastes)</em></p>
<ul>
<li><strong>Immediate Feedback</strong>:  Yeah, the default output gives me an &#8220;F&#8221;, but what am I supposed to do with an &#8220;F&#8221;?  I won&#8217;t know where and what that &#8220;F&#8221; was until the end of the entire test run, when the summary is dumped.  When running through a (functional) test suite that takes three minutes or even fifteen minutes, that just doesn&#8217;t work.</li>
<li><strong>Concise output</strong>: if everything is passing, I don&#8217;t need to see 4000+ dots displayed across my screen.  It would be nice if the entire output would fit into a single line (when there are no failures).</li>
<li><strong>Percentage</strong>:  A nice to have, not a need to have.  But knowing how many tests have already run is generally not as interesting to me as how many tests are there total, and what percentage of them have run.</li>
<li><strong>ETA</strong>: This isn&#8217;t so important for the short test runs that I do over and over again during the BDD cycle&#8230; because I expect those tests to finish in under 10 seconds anyway.  But anything more than 10 seconds, and I get distracted.  An ETA helps me limit my distraction.</li>
</ul>
<h3>So what do I do about it?</h3>
<p>Basically, what I want is a progress bar and that the errors and warnings be displayed immediately. I also want warnings to be printed for slow specs. When using color, I want the entire progress bar printed in green if everything is good, yellow if there has been a warning, and red if there has been an error.</p>
<p>Fortunately, rspec makes it <em>very</em> simple to write a custom output formatter. So, a couple of weekends ago, I threw something together in about half an hour.  I&#8217;ve used it ever since, and I&#8217;m pretty happy with it (I&#8217;ve tweaked it a little bit).</p>
<h3>What does it look like?</h3>
<p>A static image doesn&#8217;t really show off the progress bar very well, but here&#8217;s a screenshot (click thumbnail for full-size):</p>
<p style="text-align: center;"><a href="http://ekenosen.net/nick/devblog-uploads/2008/12/rspec_progress_bar_example.jpg"><img class="size-medium wp-image-87 aligncenter" title="rspec_progress_bar_example" src="http://ekenosen.net/nick/devblog-uploads/2008/12/rspec_progress_bar_example-300x276.jpg" alt="" width="300" height="276" /></a></p>
<p>And here is a video, so you can see it in action (very low quality, sorry):</p>
<p style="text-align: center;"><embed id="VideoPlayback" src="http://video.google.com/googleplayer.swf?docid=-6454522180447850859&#038;hl=en&#038;fs=true" style="width:400px;height:326px" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"> </embed></p>
<h3>The code</h3>
<p><a href="http://ekenosen.net/bzr/rspec_support/compact_progress_bar_formatter.rb">http://ekenosen.net/bzr/rspec_support/compact_progress_bar_formatter.rb</a></p>
<p>It&#8217;s currently hosted in a <a href="http://bazaar-vcs.org/">bzr</a> branch, but I&#8217;ll stick it up on github if anyone really cares about that.  Also, it depends upon the progressbar gem, so you&#8217;ll need to install that.</p>
<h3>To use it</h3>
<p>The location you download the script to is not important, but this is where I&#8217;m (currently) keeping it. You could also use bzr to pull down the entire branch.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ gem install progressbar
&nbsp;
$ mkdir -p ~/lib/rspec/
$ cd ~/lib/rspec
$ wget http://ekenosen.net/bzr/rspec_support/compact_progress_bar_formatter.rb
&nbsp;
$ spec --require ~/lib/rspec/compact_progress_bar_formatter.rb -c -f Spec::Runner::Formatter::CompactProgressBarFormatter path/to/specs</pre></div></div>

<p>I&#8217;m currently using some bash aliases to keep the spec command line nice and short.  Perhaps there&#8217;s a better way?  In <code>~/.bash_aliases</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">alias  spb='spec        --require ~/lib/rspec/compact_progress_bar_formatter.rb --format Spec::Runner::Formatter::CompactProgressBarFormatter --color'
alias sspb='script/spec --require ~/lib/rspec/compact_progress_bar_formatter.rb --format Spec::Runner::Formatter::CompactProgressBarFormatter --color'</pre></div></div>

<p>For rake tasks, I use a <code>spec/local_spec.opts</code> (<code>spec/spec.opts</code> is checked into version control as a default).</p>
<h3>Nitpicking</h3>
<p>I cheated: I didn&#8217;t develop it BDD-style.  I intend to fix that soon by rewriting it spec-first; certainly before adding any more functionality. <a href="http://rubyhoedown2008.confreaks.com/05-bryan-liles-lightning-talk-tatft-test-all-the-f-in-time.html">TATFT</a> indeed.</p>
<p>I&#8217;d really like to be able to send the threshold (for reporting of slow specs) in on the command line.  It might be easy to do, I haven&#8217;t looked into it&#8230; I&#8217;m currently just editing the file every time I want to change the threshold (e.g. normal specs run with a threshold of 0.1 sec, functional specs run with a threshold of 5 or 10 seconds).  Messy.</p>
<p>Also, it would be nice if the command line didn&#8217;t need to be so long&#8230; perhaps a supported gem/plugin system for rspec?  If that were easily doable, then I would probably package this up into a gem.</p>
<p>I had to break into the progressbar&#8217;s internals a little bit&#8230; it didn&#8217;t publically expose all of the functionality I wanted.  Still, that&#8217;s better than creating my own progressbar library.</p>
]]></content:encoded>
			<wfw:commentRss>http://ekenosen.net/nick/devblog/2008/12/better-progress-bar-for-rspec/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
