Just A Summary : Tag rspec, everything about rspec http://www.bofh.org.uk/articles/tag/rspec.rss en-us 40 Piers Cawley Practices Punditry Baby's first screencast <p>If you follow the Ruby blogs, you will probably have seen a bunch of programmers attempting to do something akin to Haskell&#8217;s <code>maybe</code>, or the ObjectiveC style, message eating null.</p> <p>Generally, by about the 3rd time you&#8217;ve written</p> <div class="typocode"><pre><code class="typocode_default ">if foo.nil? ? nil : foo.bar ... end</code></pre></div> <p>you&#8217;re getting pretty tired of it. Especially when <code>foo</code> is a variable you&#8217;ve had to introduce solely to hold a value while you check that it&#8217;s not nil. The pain really kicks in when you really want to call <code>foo.bar.baz</code>. You can end up writing monstrosities like <code>(tmp = foo.nil? ? nil : foo.bar).nil? ? nil : tmp.baz</code> (actually, if you were to write that in production code, you probably have bigger problems). One option is to just define <code>NilClass#method_missing</code> to behave like its Objective C equivalent, but I&#8217;ve never quite had the nerve to find out how that might work. I wanted to write</p> <div class="typocode"><pre><code class="typocode_default ">if maybe { foo.bar.baz } ... end</code></pre></div> <p>and have nil behave like an Objective C nil for the duration of the block, but no longer. So I wrote it. Then I thought about how to present it. I wrote the thing test first using rspec and the whole thing just flowed, but writing up a test first development process for a blog entry is painful, so I&#8217;ve made a very rough (but blessedly short) screencast of the process instead.</p> <p><a href="http://www.archive.org/download/RubyMaybeRoughcut/Monad2.mov"><img src="http://www.bofh.org.uk/images/monadcast.jpg" alt="" /></a></p> <p>That&#8217;s a slightly reduced thumbnail, the movie is substantially more readable. The bottom pane of the window is the output of autotest rerunning the spec every time either the spec or the implementation changes. The top pane alternates between the specs and the implementation. Generally, every time I edit the specs, a test starts failing and every time I edit the implementation it starts passing again. In the (any) real coding run, there were of course false starts, but generally the specs kept me pretty straight.</p> <p>A word or two of warning: This is a completely unedited, silent, screen cast, there are typos, backtrackings and other embarrassments. I stopped recording once I&#8217;d got 4 tests passing, but this is far from release quality (it&#8217;s perfectly usable if you know its limitations, but it&#8217;s not entirely robust).</p> <p>Please let me know what you think of this. I&#8217;m aiming to make a more polished version, complete with voice over and it would be good to know which bits are confusing and need addressing in more detail in the voice over.</p> Fri, 14 Mar 2008 14:29:00 -0500 urn:uuid:4b5ad7c3-d601-487b-9554-2d140f18b1a8 pdcawley@bofh.org.uk (Piers Cawley) http://www.bofh.org.uk/articles/2008/03/14/babys-first-screencast#comments Ruby screencast maybe rspec TDD http://www.bofh.org.uk/articles/2008/03/14/babys-first-screencast Getting the Rspec religion <p>I&#8217;ve been eyeing the <a href="http://rspec.rubyforge.org/index.html">rspec</a> and rspec on rails packages and thinking I should give them a go.</p> <p>To my eye at least, something like:</p> <div class="typocode"><pre><code class="typocode_default ">context 'Given a published article' do fixtures :contents setup { @article = contents(:published_article) } specify 'changing content invalidates the cache' do @article.body = 'new body' @article.invalidates_cache?.should_be true end end context 'Given an unpublished article' do fixtures :contents setup { @article = contents(:unpublished_article) } specify 'changing content keeps the cache' do @article.body = 'new body' @article.invalidates_cache?.should_be false end end</code></pre></div> <p>reads far more fluently than the equivalent Test::Unit based tests:</p> <div class="typocode"><pre><code class="typocode_default ">class CacheSupportTest &lt; Test::Unit::TestCase fixtures :contents def test_changing_published_article_invalidates_the_cache art = contents(:published_article) art.body = 'new body' assert art.invalidates_cache? end def test_changing_unpublished_article_keeps_the_cache art = contents(:unpublished_article) art.body = 'new body' assert ! art.invalidates_cache? end end</code></pre></div> <p>So, I installed everything and started to work on a new class in Typo using rspec. Rather annoyingly, this seemed to break the current test suite, so instead of working on my new model class, I set to porting the existing suite.</p> <p>And, on about my third test suite, I found what I think is a bug in the suite. I&#8217;m not <em>sure</em> it&#8217;s a bug, because, the way the test is written (by me, I admit it), masks the intent quite dramatically. I&#8217;m also finding that the freedom to name specifications and contexts in English rather than <code>method_names_that_go_on_for_ever</code> is forcing me to come up with much more useful descriptions of what I&#8217;m testing. I find myself working on making the spec runner output read reasonably well as English, and doing that casts light on what is and isn&#8217;t being tested.</p> <p>I&#8217;ve known for a while that Typo&#8217;s test suite is, um, spotty, but the porting process is really helping me get familiar with what&#8217;s being tested. I&#8217;m half tempted to start adding extra specs as I go, and if I could work out how to keep the existing tests working while I did it, I would, but my priority for now is to get to the point where I can check the specs and be confident that the new specs are no worse than the old tests.</p> <p>Because I&#8217;m much more confident that I know what the specs are <em>doing</em>, I&#8217;m also confident that it won&#8217;t be hard to revisit them to help specify typo&#8217;s behaviour better. I&#8217;ll just have to give myself the discipline of beginning each coding session with half an hour of fleshing out the specifications before I get back to adding behaviour.</p> Sun, 26 Nov 2006 02:52:00 -0600 urn:uuid:30163f70-ce19-4a05-8e66-e6532c674385 pdcawley@bofh.org.uk (Piers Cawley) http://www.bofh.org.uk/articles/2006/11/26/getting-the-rspec-religion#comments The Practice of Programming Ruby Typo rspec http://www.bofh.org.uk/articles/2006/11/26/getting-the-rspec-religion