<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[David Chelimsky]]></title>
  <link href="http://blog.davidchelimsky.net/atom.xml" rel="self"/>
  <link href="http://blog.davidchelimsky.net/"/>
  <updated>2016-08-18T07:48:02-05:00</updated>
  <id>http://blog.davidchelimsky.net/</id>
  <author>
    <name><![CDATA[David]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Myron Marston and Andy Lindeman are RSpec's new project leads]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/11/28/myron-marston-and-andy-lindeman-are-rspecs-new-project-leads/"/>
    <updated>2012-11-28T11:16:00-06:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/11/28/myron-marston-and-andy-lindeman-are-rspecs-new-project-leads</id>
    <content type="html"><![CDATA[<h2>TL;DR</h2>

<p>Myron Marston is taking over leadership of the RSpec project, and will
be the lead maintainer of the rspec-core, rspec-expectations, and
rspec-mocks gems.</p>

<p>Andy Lindeman is taking over as lead maintainer of the rspec-rails gem.</p>

<h3>Myron Marston is RSpec&rsquo;s new project lead</h3>

<p>Myron Marston has been contributing to RSpec since the ramp up to the
2.0 release in 2010, and joined the core team in early 2011. In addition
to solid contributions to the code base, Myron has taken responsibility
for many bug reports, feature requests, pull requests, etc. He also
makes a habit of answering RSpec-related questions on Stack Overflow and
Twitter, and he does this all with thoughtfulness, patience, and
wisdom.</p>

<p>I can&rsquo;t think of a better choice to lead the RSpec project, so I invited
him to do so. Thankfully, <a href="http://www.seomoz.org/">SEOmoz</a>, Myron&rsquo;s
employer, is allowing him to work on RSpec during work hours, and so he
accepted.</p>

<p>In addition to the overall project lead, Myron will be the lead
maintainer of the rspec-core, rspec-expectations, and rspec-mocks
gems. While Myron uses these core rspec libs every day for his work,
he doesn&rsquo;t do much with Rails, so we discussed and agreed that we&rsquo;d ask
somebody else to take care of the rspec-rails gem. Enter Andy Lindeman.</p>

<h3>Andy Lindeman is the new lead maintainer of rspec-rails</h3>

<p>Andy Lindeman is the newest addition to the rspec core team. Like Myron,
Andy takes great care in shepherding pull requests and answering
questions in addition to making his own rock solid contributions.  Andy
also writes Rails apps and does Rails training at
<a href="http://bignerdranch.com/">Big Nerd Ranch</a>, which puts him in a great
position to ensure that rspec-rails keeps up with changes in Rails
remains a great choice for developing Rails apps.</p>

<p>Thanks in part to <a href="http://bignerdranch.com/">Big Nerd Ranch</a> for their
support of Andy&rsquo;s work on RSpec, Andy has agreed to take the lead on
rspec-rails.</p>

<h3>As for me &hellip;</h3>

<p>When Dave Astels introduced Steven Baker&rsquo;s new RSpec library to me back
in 2005, I started submitting patches, Steven gave me commit rights and,
a year later, he decided to move on to other work and offered me
leadership of the project.  I was overjoyed to accept.</p>

<p>In those days, I was teaching TDD/Refactoring courses for Object
Mentor. I was encouraged to work on OSS in my bench time, and I was
particularly interested in tools that helped to promote the
documentation and design aspects of TDD. RSpec and some of the early
definitions/discussions of BDD (&ldquo;it&rsquo;s all behavior!&rdquo;) fit perfectly into
my thinking and my world.</p>

<p>After I left Object Mentor, RSpec was part of my day to day work on Ruby
applications, but most of my work on its maintenance moved to my spare
time. This was fine at first, as I had the support of employers and
family, but I found myself doing less and less of pretty much everything
else that I enjoy and learn from.</p>

<p>Back in June, I joined a project team at DRW Trading that deals in a lot
of Clojure and almost no Ruby.  In the roughly 6 months since, I&rsquo;ve been
learning a new domain, a new language, new programming models, and even
a new text editor. I consider myself extraordinarily fortunate to be
able to learn all of these new things, but I&rsquo;ve found myself less and
less able to balance my work on RSpec with my job and with everything
else I want to do.</p>

<p>And so, it is time.</p>

<p>Of course, I&rsquo;ll continue to contribute to the project and surrounding
conversation, and I look forward to seeing all my friends in the
community at assorted conferences in the coming year(s). I&rsquo;ll just
be arriving without my RSpec Lead hat on.</p>

<p>Thank you to Steven Baker for handing me the wheel, and thank you to
everybody who has participated in the project and all RSpec users for
your support over these 6+ years. It&rsquo;s been a great honor and a great
pleasure.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-2.12 is released]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/11/12/rspec-212-is-released/"/>
    <updated>2012-11-12T22:37:04-06:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/11/12/rspec-212-is-released</id>
    <content type="html"><![CDATA[<p>rspec-2.12 is a minor release (per <a href="http://semver.org/">SemVer</a>), which
includes numerous enhancements and bug fixes. It is fully backward compatible
with previous rspec-2 releases and is a recommended upgrade for all users.</p>

<p>Thanks to all who contributed. Special thanks to Myron Marston and Andy
Lindeman for their personal contributions to the code as well as a great job
shepherding pull requests from several new contributors.</p>

<p>UPDATE: If you&rsquo;re an rspec/rails/capybara user, be sure to read <a href="http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html">Andy Lindeman&rsquo;s blog post on Capybara-2.0 and rspec-rails</a>.</p>

<h2>Docs</h2>

<h3>RDoc</h3>

<ul>
<li><a href="http://rubydoc.info/gems/rspec-core">http://rubydoc.info/gems/rspec-core</a></li>
<li><a href="http://rubydoc.info/gems/rspec-expectations">http://rubydoc.info/gems/rspec-expectations</a></li>
<li><a href="http://rubydoc.info/gems/rspec-mocks">http://rubydoc.info/gems/rspec-mocks</a></li>
<li><a href="http://rubydoc.info/gems/rspec-rails">http://rubydoc.info/gems/rspec-rails</a></li>
</ul>


<h3>Cucumber features</h3>

<ul>
<li><a href="http://relishapp.com/rspec/rspec-core">http://relishapp.com/rspec/rspec-core</a></li>
<li><a href="http://relishapp.com/rspec/rspec-expectations">http://relishapp.com/rspec/rspec-expectations</a></li>
<li><a href="http://relishapp.com/rspec/rspec-mocks">http://relishapp.com/rspec/rspec-mocks</a></li>
<li><a href="http://relishapp.com/rspec/rspec-rails">http://relishapp.com/rspec/rspec-rails</a></li>
</ul>


<h2>Release notes</h2>

<h3>rspec-core-2.12.0</h3>

<p><a href="http://github.com/rspec/rspec-core/compare/v2.11.1...v2.12.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Add support for custom ordering strategies for groups and examples.
(Myron Marston)</li>
<li>JSON Formatter (Alex Chaffee)</li>
<li>Refactor rake task internals (Sam Phippen)</li>
<li>Refactor HtmlFormatter (Pete Hodgson)</li>
<li>Autotest supports a path to Ruby that contains spaces (dsisnero)</li>
<li>Provide a helpful warning when a shared example group is redefined.
(Mark Burns).</li>
<li><code>--default_path</code> can be specified as <code>--default-path</code>. <code>--line_number</code> can be
specified as <code>--line-number</code>. Hyphens are more idiomatic command line argument
separators (Sam Phippen).</li>
<li>A more useful error message is shown when an invalid command line option is
used (Jordi Polo).</li>
<li>Add <code>format_docstrings { |str| }</code> config option. It can be used to
apply formatting rules to example group and example docstrings.
(Alex Tan)</li>
<li>Add support for an <code>.rspec-local</code> options file. This is intended to
allow individual developers to set options in a git-ignored file that
override the common project options in <code>.rspec</code>. (Sam Phippen)</li>
<li>Support for mocha 0.13.0. (Andy Lindeman)</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Remove override of <code>ExampleGroup#ancestors</code>. This is a core ruby method that
RSpec shouldn&rsquo;t override. Instead, define <code>ExampleGroup#parent_groups</code>. (Myron
Marston)</li>
<li>Limit monkey patching of shared example/context declaration methods
(<code>shared_examples_for</code>, etc.) to just the objects that need it rather than
every object in the system (Myron Marston).</li>
<li>Fix Metadata#fetch to support computed values (Sam Goldman).</li>
<li>Named subject can now be referred to from within subject block in a nested
group (tomykaira).</li>
<li>Fix <code>fail_fast</code> so that it properly exits when an error occurs in a
<code>before(:all) hook</code> (Bradley Schaefer).</li>
<li>Make the order spec files are loaded consistent, regardless of the
order of the files returned by the OS or the order passed at
the command line (Jo Liss and Sam Phippen).</li>
<li>Ensure instance variables from <code>before(:all)</code> are always exposed
from <code>after(:all)</code>, even if an error occurs in <code>before(:all)</code>
(Sam Phippen).</li>
<li><code>rspec --init</code> no longer generates an incorrect warning about <code>--configure</code>
being deprecated (Sam Phippen).</li>
<li>Fix pluralization of <code>1 seconds</code> (Odin Dutton)</li>
<li>Fix ANSICON url (Jarmo Pertman)</li>
<li>Use dup of Time so reporting isn&rsquo;t clobbered by examples that modify Time
without properly restoring it. (David Chelimsky)</li>
</ul>


<p>Deprecations</p>

<ul>
<li><code>share_as</code> is no longer needed. <code>shared_context</code> and/or
<code>RSpec::SharedContext</code> provide better mechanisms (Sam Phippen).</li>
<li>Deprecate <code>RSpec.configuration</code> with a block (use <code>RSpec.configure</code>).</li>
</ul>


<h3>rspec-expectations-2.12.0</h3>

<p><a href="http://github.com/rspec/rspec-expectations/compare/v2.11.3...v2.12.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Colorize diffs if the <code>--color</code> option is configured. (Alex Coplan)</li>
<li>Include backtraces in unexpected errors handled by <code>raise_error</code>
matcher (Myron Marston)</li>
<li>Print a warning when users accidentally pass a non-string argument
as an expectation message (Sam Phippen)</li>
<li><code>=~</code> and <code>match_array</code> matchers output a more useful error message when
the actual value is not an array (or an object that responds to <code>#to_ary</code>)
(Sam Phippen)</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Fix <code>include</code> matcher so that <code>expect({}).to include(:a =&gt; nil)</code>
fails as it should (Sam Phippen).</li>
<li>Fix <code>be_an_instance_of</code> matcher so that <code>Class#to_s</code> is used in the
description rather than <code>Class#inspect</code>, since some classes (like
<code>ActiveRecord::Base</code>) define a long, verbose <code>#inspect</code>.
(Tom Stuart)</li>
</ul>


<h3>rspec-mocks-2.12.0</h3>

<p><a href="http://github.com/rspec/rspec-mocks/compare/v2.11.3...v2.12.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li><code>and_raise</code> can accept an exception class and message, more closely
matching <code>Kernel#raise</code> (e.g., <code>foo.stub(:bar).and_raise(RuntimeError, "message")</code>)
(Bas Vodde)</li>
<li>Add <code>and_call_original</code>, which will delegate the message to the
original method (Myron Marston).</li>
</ul>


<p>Deprecations:</p>

<ul>
<li>Add deprecation warning when using <code>and_return</code> with <code>should_not_receive</code>
(Neha Kumari)</li>
</ul>


<h3>rspec-rails-2.12.0</h3>

<p><a href="http://github.com/rspec/rspec-rails/compare/v2.11.4...v2.12.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Support validation contexts when using <code>#errors_on</code> (Woody Peterson)</li>
<li>Include RequestExampleGroup in groups in spec/api</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Add <code>should</code> and <code>should_not</code> to <code>CollectionProxy</code> (Rails 3.1+) and
<code>AssociationProxy</code> (Rails 3.0).  (Myron Marston)</li>
<li><code>controller.controller_path</code> is set correctly for view specs in Rails 3.1+.
(Andy Lindeman)</li>
<li>Generated specs support module namespacing (e.g., in a Rails engine).
(Andy Lindeman)</li>
<li><code>render</code> properly infers the view to be rendered in Rails 3.0 and 3.1
(John Firebaugh)</li>
<li>AutoTest mappings involving config/ work correctly (Brent J. Nordquist)</li>
<li>Failures message for <code>be_new_record</code> are more useful (Andy Lindeman)</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Agile Testing and BDD eXchange 2012 in NYC]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/09/22/agile-testing-and-bdd-exchange-2012-in-nyc/"/>
    <updated>2012-09-22T10:04:00-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/09/22/agile-testing-and-bdd-exchange-2012-in-nyc</id>
    <content type="html"><![CDATA[<p>I&rsquo;m excited to be presenting on Behavior-Driven Objects at the Agile Testing &amp; BDD eXchange in NYC on Monday, October 1st (details below). From the <a href="http://skillsmatter.com/podcast/agile-scrum/david-chelimsky-talk-tbc">conference website</a>:</p>

<blockquote><p>One of the original ideas behind BDD was that testing should be about behavior at all levels. Effectively, all automated tests can be viewed as &ldquo;functional tests&rdquo; of entry points, be they user facing or internal, crossing procedural boundaries or not. In this talk we&rsquo;ll explore approaches to focusing on the behavior of objects, using language that also serves to document the expected behavior in ways useful to both technical and non-technical stakeholders.</p></blockquote>

<p>The Agile Testing &amp; BDD eXchange is a one-day conference with talks, open-space discussions and focus on &lsquo;quality time&rsquo; between presenters and attendees. There is a nice blend of technical talks like <a href="http://skillsmatter.com/podcast/agile-testing/the-solid-principles-of-test-design">Uncle Bob Martin talking about how to apply SOLID principles to test design</a>, and more process oriented talks like <a href="http://skillsmatter.com/podcast/agile-testing/ellen-gottesdiener-talk-tbc">Ellen Gottesdiener&rsquo;s talk on product definition through structured conversation</a>.</p>

<p>You can read the full program and purchase tickets at <a href="http://bit.ly/S0Ypb8">http://bit.ly/S0Ypb8</a> (be sure to use the promo-code &ldquo;bddxnyc-community-discount&rdquo;).</p>

<p>What: Agile Testing &amp; BDD eXchange NYC</p>

<p>When: October 1st 2012</p>

<p>Where: The Ace Hotel NYC</p>

<p>Twitter: #BDDXNYC</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-2.11 is released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/07/07/rspec-211-is-released/"/>
    <updated>2012-07-07T17:00:42-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/07/07/rspec-211-is-released</id>
    <content type="html"><![CDATA[<p>rspec-2.11.0 is out and filled with a bunch of new features.  Big thanks to all
who contributed, especially <a href="http://twitter.com/justinko">Justin Ko</a>, <a href="http://alindeman.github.com/">Andy
Lindeman</a> (the newest addition to the RSpec core
team) and <a href="http://myronmars.to/n/dev-blog">Myron Marston</a> for their great job
addressing issues and shepherding pull requests.</p>

<p>Thanks also to Myron for all his work on two great new features: <a href="http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax">the new
expectation
syntax</a>
and <a href="http://myronmars.to/n/dev-blog/2012/06/constant-stubbing-in-rspec-2-11">support for stubbing
constants</a>.</p>

<h3>rspec-core-2.11.0</h3>

<p><a href="http://github.com/rspec/rspec-core/compare/v2.10.1...v2.11.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Support multiple <code>--example</code> options. (Daniel Doubrovkine @dblock)</li>
<li>Named subject e.g. <code>subject(:article) { Article.new }</code>

<ul>
<li>see <a href="http://blog.davidchelimsky.net/2012/05/13/spec-smell-explicit-use-of-subject/">http://blog.davidchelimsky.net/2012/05/13/spec-smell-explicit-use-of-subject/</a>
for background.</li>
<li>thanks to Bradley Schaefer for suggesting it and Avdi Grimm for almost
suggesting it.</li>
</ul>
</li>
<li><code>config.mock_with</code> and <code>config.expect_with</code> yield custom config object to a
block if given

<ul>
<li>aids decoupling from rspec-core&rsquo;s configuation</li>
</ul>
</li>
<li><code>include_context</code> and <code>include_examples</code> support a block, which gets eval&rsquo;d
in the current context (vs the nested context generated by <code>it_behaves_like</code>).</li>
<li>Add <code>config.order = 'random'</code> to the <code>spec_helper.rb</code> generated by <code>rspec
--init</code>.</li>
<li>Delay the loading of DRb (Myron Marston).</li>
<li>Limit monkey patching of <code>describe</code> onto just the objects that need it rather
than every object in the system (Myron Marston).</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Support alternative path separators. For example, on Windows, you can now do
this: <code>rspec spec\subdir</code>. (Jarmo Pertman @jarmo)</li>
<li>When an example raises an error and an after or around hook does as
well, print out the hook error. Previously, the error was silenced and
the user got no feedback about what happened. (Myron Marston)</li>
<li><code>--require</code> and <code>-I</code> are merged among different configuration sources (Andy
Lindeman)</li>
<li>Delegate to mocha methods instead of aliasing them in mocha adapter.</li>
</ul>


<h3>rspec-expectations-2.11.0</h3>

<p><a href="http://github.com/rspec/rspec-expectations/compare/v2.10.0...v2.11.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Expand <code>expect</code> syntax so that it supports expections on bare values
in addition to blocks (Myron Marston).</li>
<li>Add configuration options to control available expectation syntaxes
(Myron Marston):

<ul>
<li><code>RSpec.configuration.expect_with(:rspec) { |c| c.syntax = :expect }</code></li>
<li><code>RSpec.configuration.expect_with(:rspec) { |c| c.syntax = :should }</code></li>
<li><code>RSpec.configuration.expect_with(:rspec) { |c| c.syntax = [:should, :expect] }</code></li>
<li><code>RSpec.configuration.add_should_and_should_not_to Delegator</code></li>
</ul>
</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Allow only <code>Numeric</code> values to be the &ldquo;actual&rdquo; in the <code>be_within</code> matcher.
This prevents confusing error messages. (Su Zhang @zhangsu)</li>
<li>Define <code>should</code> and <code>should_not</code> on <code>BasicObject</code> rather than <code>Kernel</code>
on 1.9. This makes <code>should</code> and <code>should_not</code> work properly with
<code>BasicObject</code>-subclassed proxy objects like <code>Delegator</code>. (Myron
Marston)</li>
</ul>


<h3>rspec-mocks-2.11.0</h3>

<p><a href="http://github.com/rspec/rspec-mocks/compare/v2.10.1...v2.11.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>expose ArgumentListMatcher as a formal API

<ul>
<li>supports use by 3rd party mock frameworks like Surrogate</li>
</ul>
</li>
<li>Add <code>stub_const</code> API to stub constants for the duration of an
example (Myron Marston).</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Fix regression of edge case behavior. <code>double.should_receive(:foo) { a }</code>
was causing a NoMethodError when <code>double.stub(:foo).and_return(a, b)</code>
had been setup before (Myron Marston).</li>
<li>Infinite loop generated by using <code>any_instance</code> and <code>dup</code>. (Sidu Ponnappa @kaiwren)</li>
<li><code>double.should_receive(:foo).at_least(:once).and_return(a)</code> always returns a
even if <code>:foo</code> is already stubbed.</li>
<li>Prevent infinite loop when interpolating a null double into a string
as an integer (<code>"%i" % double.as_null_object</code>). (Myron Marston)</li>
<li>Fix <code>should_receive</code> so that null object behavior (e.g. returning
self) is preserved if no implementation is given (Myron Marston).</li>
<li>Fix <code>and_raise</code> so that it raises <code>RuntimeError</code> rather than
<code>Exception</code> by default, just like ruby does. (Andrew Marshall)</li>
</ul>


<h3>rspec-rails-2.11.0</h3>

<p><a href="http://github.com/rspec/rspec-rails/compare/v2.10.1...v2.11.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>The generated <code>spec/spec_helper.rb</code> sets <code>config.order = "random"</code> so that
specs run in random order by default.</li>
<li>rename <code>render_template</code> to <code>have_rendered</code> (and alias to <code>render_template</code>
for backward compatibility)</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>&ldquo;uninitialized constant&rdquo; errors are avoided when using using gems like
<code>rspec-rails-uncommitted</code> that define <code>Rspec::Rails</code> before <code>rspec-rails</code>
loads (Andy Lindeman)</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Spec smell: explicit use of subject]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/05/13/spec-smell-explicit-use-of-subject/"/>
    <updated>2012-05-13T21:13:46-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/05/13/spec-smell-explicit-use-of-subject</id>
    <content type="html"><![CDATA[<h2>TL;DR</h2>

<p>Explicit use of the &ldquo;subject&rdquo; abstraction is a code smell, and should be
refactored to use a more intention revealing name whenever possible.</p>

<h2>One liners</h2>

<p>rspec-core supports a one-liner syntax to reduce the noise of common
requirements like validations:</p>

<figure class='code'><figcaption><span>article_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Without support for this syntax, the same example might look like this:</p>

<figure class='code'><figcaption><span>article_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;validates presence of :title&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">article</span> <span class="o">=</span> <span class="no">Article</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'>    <span class="n">article</span><span class="o">.</span><span class="n">should</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The benefit of this more verbose example is that it we can read it and
understand all the parts right away: an object is initialized and assigned to a
local variable, then that variable is used for an expectation.</p>

<p>The benefit of the one-liner is that it&rsquo;s terse and expressive, but that comes
at a cost: you can&rsquo;t see what the expectation is being evaluated against, so you
have to understand some underlying mechanics in order to isolate/understand a
failure.</p>

<h2>The <code>subject</code> abstraction</h2>

<p>Behind the scenes, the one-liner uses a &ldquo;subject&rdquo; abstraction supported by two
methods named <code>subject</code>. One is a class method on <code>ExampleGroup</code>, used to
declare the &ldquo;subject&rdquo; of all of the examples in the group:</p>

<figure class='code'><figcaption><span>article_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">subject</span> <span class="p">{</span> <span class="no">Article</span><span class="o">.</span><span class="n">new</span> <span class="p">}</span>
</span><span class='line'>  <span class="c1"># ...</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The other is an instance method on <code>ExampleGroup</code>. The first time it is called
within an example the block passed to the class&#8217; <code>subject</code> method is evaluated
and its result memoized, returning the same value from that and each subsequent
<code>subject</code> call:</p>

<figure class='code'><figcaption><span>article_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="c1"># ...</span>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;validates presence of :title&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">subject</span><span class="o">.</span><span class="n">should</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here is what they look like together:</p>

<figure class='code'><figcaption><span>article_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># not recommended</span>
</span><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">subject</span> <span class="p">{</span> <span class="no">Article</span><span class="o">.</span><span class="n">new</span> <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;validates presence of :title&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">subject</span><span class="o">.</span><span class="n">should</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The problem with this example is that the word &ldquo;subject&rdquo; is not very intention
revealing.  That might not appear problematic in this small example because you
can see the declaration on line 3 and the reference on line 6.  But when this
group grows to where you have to scroll up from the reference to find the
declaration, the generic nature of the word &ldquo;subject&rdquo; becomes a hinderance to
understanding and slows you down.</p>

<p>In this case, we&rsquo;d be better served by using a method (or a <code>let</code> declaration)
with an intention revealing name:</p>

<figure class='code'><figcaption><span>article_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">article</span><span class="p">;</span> <span class="no">Article</span><span class="o">.</span><span class="n">new</span><span class="p">;</span> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;validates presence of :title&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">article</span><span class="o">.</span><span class="n">should</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>If we can do that, you might wonder why we have &ldquo;subject&rdquo; at all.  Well, it was
originally designed to never be seen:</p>

<h2>Implicit subject</h2>

<p>Note in the example with <code>subject { Article.new }</code>, that the <code>subject</code> declaration is initializing an
<code>Article</code> with no args.  Since RSpec knows that the first argument to
<code>describe</code> is the <code>Article</code> class, it can store a similar block in the
background as a default, implicit subject declaration, leaving us with:</p>

<figure class='code'><figcaption><span>article_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># refactoring toward implicit step 1</span>
</span><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;validates presence of :title&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">subject</span><span class="o">.</span><span class="n">should</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>That&rsquo;s a little better, but now <code>subject</code> appears out of nowhere in the
example, leaving the reader to wonder where it came from.  To remove the need
for explicitly referencing <code>subject</code>, the example delegates <code>should</code> and
<code>should_not</code> to <code>subject</code> when it is, itself, the receiver:</p>

<figure class='code'><figcaption><span>article_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># refactoring toward implicit step 2</span>
</span><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;validates presence of :title&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">should</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Starting that line with &ldquo;should&rdquo; seems a bit odd though, so the final step is to
do it all in one line:</p>

<figure class='code'><figcaption><span>article_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we have a completely implicit subject, and the result reads quite nicely in
both the code and the output when run with <code>--format documentation</code>:</p>

<pre><code>Article
  should validate presence of :title
</code></pre>

<p>We still need to trust that something is doing some work for us but it&rsquo;s all
operating at the same level of abstraction, so we don&rsquo;t have to try to
interpret half of the functionality.</p>

<h2>Avoid explicit use of <code>subject</code></h2>

<p>Intention revealing names are crucial if you want to be able to quickly scan
and understand code as you navigate around different parts of a system. This is
as true for specs as it is for implementation, and the generic nature of the
word &ldquo;subject&rdquo; makes it a poor choice when a more intention revealing name can
be used.</p>

<h2>Is an explicit <code>subject</code> ever OK?</h2>

<p>Guidelines are guidelines; YMMV. In general I would recommend that if there is
a reasonable way to use an intention revealing name instead of &ldquo;subject&rdquo;, you
should. The only use case I can think of in RSpec in which another name can&rsquo;t
be used is the one liner syntax:</p>

<figure class='code'><figcaption><span>american_citizen_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">AmericanCitizen</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">context</span> <span class="s2">&quot;18 years of age&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">subject</span> <span class="p">{</span> <span class="no">Person</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:birthdate</span> <span class="o">=&gt;</span> <span class="mi">18</span><span class="o">.</span><span class="n">years</span><span class="o">.</span><span class="n">ago</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>    <span class="n">it</span> <span class="p">{</span> <span class="n">should</span>     <span class="n">be_able_to</span><span class="p">(</span><span class="ss">:vote</span><span class="p">)</span>   <span class="p">}</span>
</span><span class='line'>    <span class="n">it</span> <span class="p">{</span> <span class="n">should</span>     <span class="n">be_able_to</span><span class="p">(</span><span class="ss">:enlist</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'>    <span class="n">it</span> <span class="p">{</span> <span class="n">should_not</span> <span class="n">be_able_to</span><span class="p">(</span><span class="ss">:drink</span><span class="p">)</span>  <span class="p">}</span> <span class="c1"># srsly?</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here we <em>have</em> to use <code>subject</code> because that&rsquo;s the only way to tell RSpec where
to send <code>should</code> and <code>should_not</code>. In my opinion, any other explicit appearance
of <code>subject</code> can and should be refactored to use an intention revealing name.</p>

<h2>Update 2012-05-15</h2>

<p>Based on feedback on this post, I added support for a &ldquo;named subject,&rdquo; which
lets you reference the declared subject implicitly in one-liners and with an
intention revealing name in standard examples:</p>

<figure class='code'><figcaption><span>checking_account_spec.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">CheckingAccount</span><span class="p">,</span> <span class="s2">&quot;with a non-zero starting balance&quot;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">subject</span><span class="p">(</span><span class="ss">:account</span><span class="p">)</span> <span class="p">{</span> <span class="no">CheckingAccount</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="no">Money</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="ss">:USD</span><span class="p">))</span> <span class="p">}</span>
</span><span class='line'>  <span class="n">it</span> <span class="p">{</span> <span class="n">should_not</span> <span class="n">be_overdrawn</span> <span class="p">}</span>
</span><span class='line'>  <span class="n">it</span> <span class="s2">&quot;has a balance equal to the starting balance&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">account</span><span class="o">.</span><span class="n">balance</span><span class="o">.</span><span class="n">should</span> <span class="n">eq</span><span class="p">(</span><span class="no">Money</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="mi">50</span><span class="p">,</span> <span class="ss">:USD</span><span class="p">))</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will be released with rspec-core-2.11. Keep your eyes out for it!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-mocks and rspec-rails-2.10.1 are released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/05/05/rspec-mocks-and-rspec-rails-2101-are-released/"/>
    <updated>2012-05-05T01:34:08-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/05/05/rspec-mocks-and-rspec-rails-2101-are-released</id>
    <content type="html"><![CDATA[<p>These are patch releases recommended for anybody who has already upgraded to 2.10.</p>

<h3>rspec-mocks-2.10.1</h3>

<p><a href="http://github.com/rspec/rspec-mocks/compare/v2.10.0...v2.10.1">full changelog</a></p>

<p>Bug fixes</p>

<ul>
<li>fix <a href="https://github.com/rspec/rspec-mocks/issues/132">regression of edge case behavior</a>

<ul>
<li>fixed failure of <code>object.should_receive(:message).at_least(0).times.and_return value</code></li>
<li>fixed failure of <code>object.should_not_receive(:message).and_return value</code></li>
</ul>
</li>
</ul>


<h3>rspec-rails-2.10.1</h3>

<p><a href="http://github.com/rspec/rspec-rails/compare/v2.10.0...v2.10.1">full changelog</a></p>

<p>Bug fixes</p>

<ul>
<li>fix <a href="https://github.com/rspec/rspec-rails/issues/534">regression introduced in 2.10.0 that broke integration with Devise</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-2.10 is released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/05/03/rspec-210-is-released/"/>
    <updated>2012-05-03T20:29:34-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/05/03/rspec-210-is-released</id>
    <content type="html"><![CDATA[<h3>API Docs (RDoc)</h3>

<p><a href="http://rubydoc.info/gems/rspec-core">http://rubydoc.info/gems/rspec-core</a></p>

<p><a href="http://rubydoc.info/gems/rspec-expectations">http://rubydoc.info/gems/rspec-expectations</a></p>

<p><a href="http://rubydoc.info/gems/rspec-mocks">http://rubydoc.info/gems/rspec-mocks</a></p>

<p><a href="http://rubydoc.info/gems/rspec-rails">http://rubydoc.info/gems/rspec-rails</a></p>

<h3>Cucumber docs</h3>

<p><a href="http://relishapp.com/rspec/rspec-core">http://relishapp.com/rspec/rspec-core</a></p>

<p><a href="http://relishapp.com/rspec/rspec-expectations">http://relishapp.com/rspec/rspec-expectations</a></p>

<p><a href="http://relishapp.com/rspec/rspec-mocks">http://relishapp.com/rspec/rspec-mocks</a></p>

<p><a href="http://relishapp.com/rspec/rspec-rails">http://relishapp.com/rspec/rspec-rails</a></p>

<h3>rspec-core-2.10.0</h3>

<p><a href="http://github.com/rspec/rspec-core/compare/v2.9.0...v2.10.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Add <code>prepend_before</code> and <code>append_after</code> hooks (preethiramdev)

<ul>
<li>intended for extension libs</li>
<li>restores rspec-1 behavior</li>
</ul>
</li>
<li>Reporting of profiled examples (moro)

<ul>
<li>Report the total amount of time taken for the top slowest examples.</li>
<li>Report what percentage the slowest examples took from the total runtime.</li>
</ul>
</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Properly parse <code>SPEC_OPTS</code> options.</li>
<li><code>example.description</code> returns the location of the example if there is no
explicit description or matcher-generated description.</li>
<li>RDoc fixes (Grzegorz Świrski)</li>
<li>Do not modify example ancestry when dumping errors (Michael Grosser)</li>
</ul>


<h3>rspec-expectations-2.10.0</h3>

<p><a href="http://github.com/rspec/rspec-expectations/compare/v2.9.1...v2.10.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Add new <code>start_with</code> and <code>end_with</code> matchers (Jeremy Wadsack)</li>
<li>Add new matchers for specifying yields (Myron Marson):

<ul>
<li><code>expect {...}.to yield_control</code></li>
<li><code>expect {...}.to yield_with_args(1, 2, 3)</code></li>
<li><code>expect {...}.to yield_with_no_args</code></li>
<li><code>expect {...}.to yield_successive_args(1, 2, 3)</code></li>
</ul>
</li>
<li><code>match_unless_raises</code> takes multiple exception args</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Fix <code>be_within</code> matcher to be inclusive of delta.</li>
<li>Fix message-specific specs to pass on Rubinius (John Firebaugh)</li>
</ul>


<h3>rspec-mocks-2.10.0</h3>

<p><a href="http://github.com/rspec/rspec-mocks/compare/v2.9.0...v2.10.0">full changelog</a></p>

<p>Bug fixes</p>

<ul>
<li>fail fast when an <code>exactly</code> or <code>at_most</code> expectation is exceeded</li>
</ul>


<h3>rspec-rails-2.10.0</h3>

<p><a href="http://github.com/rspec/rspec-core/compare/v2.9.0...v2.10.0">full changelog</a></p>

<p>Bug fixes</p>

<ul>
<li><code>render_views</code> called in a spec can now override the config setting. (martinsvalin)</li>
<li>Fix <code>render_views</code> for anonymous controllers on 1.8.7. (hudge, mudge)</li>
<li>Eliminate use of deprecated <code>process_view_paths</code></li>
<li>Fix false negatives when using <code>route_to</code> matcher with <code>should_not</code></li>
<li><code>controller</code> is no longer nil in <code>config.before</code> hooks</li>
<li>Change <code>request.path_parameters</code> keys to symbols to match real Rails
environment (Nathan Broadbent)</li>
<li>Silence deprecation warnings in pre-2.9 generated view specs
(Jonathan del Strother)</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-expectations-2.9.1 is released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/04/03/rspec-expectations-291-is-released/"/>
    <updated>2012-04-03T14:18:10-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/04/03/rspec-expectations-291-is-released</id>
    <content type="html"><![CDATA[<p>This is a bug-fix only release, and is recommended for everybody using rspec-2.9.</p>

<p><a href="http://github.com/rspec/rspec-expectations/compare/v2.9.0...v2.9.1">full changelog</a></p>

<p>Bug fixes</p>

<ul>
<li>Provide a helpful message if the diff between two objects is empty.</li>
<li>Fix bug diffing single strings with multiline strings.</li>
<li>Fix for error with using custom matchers inside other custom matchers
(mirasrael)</li>
<li>Fix using execution context methods in nested DSL matchers (mirasrael)</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-2.9.0 is released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/03/17/rspec-290-is-released/"/>
    <updated>2012-03-17T18:10:59-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/03/17/rspec-290-is-released</id>
    <content type="html"><![CDATA[<p>rspec-2.9.0 is released wtih lots of bug fixes and a few minor feature improvements as well. Enjoy!</p>

<h3>rspec-core-2.9.0 / 2012-03-17</h3>

<p><a href="http://github.com/rspec/rspec-core/compare/v2.8.0...v2.9.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Support for &ldquo;X minutes X seconds&rdquo; spec run duration in formatter. (uzzz)</li>
<li>Strip whitespace from group and example names in doc formatter.</li>
<li>Removed spork-0.9 shim. If you&rsquo;re using spork-0.8.x, you&rsquo;ll need to upgrade
to 0.9.0.</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Restore <code>--full_backtrace</code> option</li>
<li>Ensure that values passed to <code>config.filter_run</code> are respected when running
over DRb (using spork).</li>
<li>Ensure shared example groups are reset after a run (as example groups are).</li>
<li>Remove <code>rescue false</code> from calls to filters represented as Procs</li>
<li>Ensure described_class gets the closest constant (pyromaniac)</li>
<li>In &ldquo;autorun&rdquo;, don&rsquo;t run the specs in the at_exit hook if there was an
exception (most likely due to a SyntaxError). (sunaku)</li>
<li>Don&rsquo;t extend groups with modules already used to extend ancestor groups.</li>
<li><code>its</code> correctly memoizes nil or false values (Yamada Masaki)</li>
</ul>


<h3>rspec-expectations-2.9.0 / 2012-03-17</h3>

<p><a href="http://github.com/rspec/rspec-expectations/compare/v2.8.0...v2.9.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Move built-in matcher classes to RSpec::Matchers::BuiltIn to reduce pollution
of RSpec::Matchers (which is included in every example).</li>
<li>Autoload files with matcher classes to improve load time.</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Align <code>respond_to?</code> and <code>method_missing</code> in DSL-defined matchers.</li>
<li>Clear out user-defined instance variables between invocations of DSL-defined
matchers.</li>
<li>Dup the instance of a DSL generated matcher so its state is not changed by
subsequent invocations.</li>
<li>Treat expected args consistently across positive and negative expectations
(thanks to Ralf Kistner for the heads up)</li>
</ul>


<h3>rspec-mocks-2.9.0 / 2012-03-17</h3>

<p><a href="http://github.com/rspec/rspec-mocks/compare/v2.8.0...v2.9.0">full changelog</a></p>

<p>Enhancements</p>

<ul>
<li>Support order constraints across objects (preethiramdev)</li>
</ul>


<p>Bug fixes</p>

<ul>
<li>Allow a <code>as_null_object</code> to be passed to <code>with</code></li>
<li>Pass proc to block passed to stub (Aubrey Rhodes)</li>
<li>Initialize child message expectation args to match any args (#109 &ndash;
preethiramdev)</li>
</ul>


<h3>rspec-rails-2.9.0 / 2012-03-17</h3>

<p><a href="http://github.com/rspec/rspec-rails/compare/v2.8.1...v2.9.0">full changelog</a></p>

<p>Enhancments</p>

<ul>
<li>add description method to RouteToMatcher (John Wulff)</li>
<li>Run &ldquo;db:test:clone_structure&rdquo; instead of &ldquo;db:test:prepare&rdquo; if Active Record&rsquo;s
schema format is &ldquo;:sql&rdquo;. (Andrey Voronkov)</li>
</ul>


<p>Bug fixes</p>

<ul>
<li><code>mock_model(XXX).as_null_object.unknown_method</code> returns self again</li>
<li>Generated view specs use different IDs for each attribute.</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Validations are behavior, associations are structure]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/02/12/validations-are-behavior-associations-are-structure/"/>
    <updated>2012-02-12T16:53:27-06:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/02/12/validations-are-behavior-associations-are-structure</id>
    <content type="html"><![CDATA[<h2>TL;DR:</h2>

<ul>
<li>TDD is about specifying behavior, not structure.</li>
<li>Validations are behavior, and should be specified.</li>
<li>Associations are structure, and need not be.</li>
</ul>


<h2>Disclaimer</h2>

<p>This is my personal viewpoint, though it is not mine alone. YMMV.</p>

<h2>Declarations</h2>

<p>ActiveRecord provides a declarative interface for describing the structure and
behavior of a model:</p>

<figure class='code'><figcaption><span>article.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Article</span> <span class="o">&lt;</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'>  <span class="n">validates_presence_of</span> <span class="ss">:title</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:comments</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>While syntactically similar, these two declarations do fundamentally different
things.</p>

<h2>Validations are behavior</h2>

<p>The <code>validates_presence_of :title</code> declaration changes the behavior of
the <code>save</code> method (and other methods that use <code>save</code>), and should be specified
explicitly. Here&rsquo;s an example using shoulda matchers:</p>

<figure class='code'><figcaption><span>validate_presence_of_title.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">it</span> <span class="p">{</span> <span class="n">should</span> <span class="n">validate_presence_of</span><span class="p">(</span><span class="ss">:title</span><span class="p">)</span> <span class="p">}</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Even though the matcher&rsquo;s name looks just like the likely implementation, the
<code>validate_presence_of</code> matcher specifies that you can not save an <code>Article</code>
without a non-nil value for <code>title</code>, not that the
<code>validates_presence_of(:title)</code> declaration exists.</p>

<h2>Associations are structure</h2>

<p>The <code>has_many</code> declaration exposes a <code>comments</code> method to clients that appears
to be a collection of <code>Comment</code> objects. Doing Test-Driven Development, you
would add this declaration when a specified behavior requires it e.g.</p>

<figure class='code'><figcaption><span>with_comments_by.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe</span> <span class="no">Article</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">describe</span> <span class="s2">&quot;#with_comments_by&quot;</span> <span class="k">do</span>
</span><span class='line'>    <span class="n">it</span> <span class="s2">&quot;finds articles with comments by the submitted comment_author&quot;</span> <span class="k">do</span>
</span><span class='line'>      <span class="n">article</span> <span class="o">=</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:article</span><span class="p">)</span>
</span><span class='line'>      <span class="n">article</span><span class="o">.</span><span class="n">comments</span> <span class="o">&lt;&lt;</span> <span class="no">Factory</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="ss">:comment</span><span class="p">,</span> <span class="ss">:author</span> <span class="o">=&gt;</span> <span class="s2">&quot;jdoe&quot;</span><span class="p">)</span>
</span><span class='line'>      <span class="no">Article</span><span class="o">.</span><span class="n">with_comments_by</span><span class="p">(</span><span class="s2">&quot;jdoe&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">should</span> <span class="n">eq</span><span class="p">(</span><span class="o">[</span><span class="n">article</span><span class="o">]</span><span class="p">)</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This example needs a <code>comments</code> method that returns a collection in order to
pass.  If it doesn&rsquo;t exist already (because no other example drove you to add
it), this would be all the motivation you need to introduce it. You don&rsquo;t need
an example that says <code>it "should have_many(:comments)"</code>.</p>

<h2>Testing the framework</h2>

<p>Some will argue that we don&rsquo;t need to spec validations either, suggesting that
<code>it "should validate_presence_of(:title)"</code> is testing the Rails framework,
which we trust is already tested.  If you think of TDD as a combination of
specification, documentation, and regression testing, then this argument falls
short on the specification/documentation front because the validation is
behavior and, thus, the spec should specify the validation.</p>

<p>Even if you view testing as nothing more than a safety net against regressions,
the argument still falls down in the face of refactoring. If we add a <code>Review</code>
class that also <code>has_many(:comments)</code> and <code>validates_presence_of(:title)</code>, and
we want to extract that behavior to a <code>Postable</code> module that gets included in
both <code>Article</code> and <code>Review</code>, we&rsquo;d want a regression test to fail if we failed
to include either of those declarations in the <code>Postable</code> module.</p>

<h2>But declarations are already declarative!</h2>

<p>Another argument is that declarations supply sufficient documentation. e.g. we
can look at <code>rental_contract.rb</code> and know that it validates the presence of
<code>:rentable</code>:</p>

<figure class='code'><figcaption><span>rental_contract.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">RentalContract</span> <span class="o">&lt;</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:monthly_charges</span>
</span><span class='line'>  <span class="n">has_one</span> <span class="ss">:rentable</span><span class="p">,</span> <span class="ss">:polymorphic</span> <span class="o">=&gt;</span> <span class="kp">true</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">validates_presence_of</span> <span class="ss">:rentable</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">default_monthly_charge</span>
</span><span class='line'>    <span class="n">price</span> <span class="o">/</span> <span class="n">months_applied</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is an interesting argument that I think has some merit, but I think it
would require an extraordinarily disciplined and consistent approach of using
declarations 100% of the time in model files such that each one <em>is the spec</em>
for that model, e.g.</p>

<figure class='code'><figcaption><span>contract.rb</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Contract</span> <span class="o">&lt;</span> <span class="ss">ActiveRecord</span><span class="p">:</span><span class="ss">:Base</span>
</span><span class='line'>  <span class="n">validates_presence_of</span> <span class="ss">:name</span>
</span><span class='line'>  <span class="n">has_many</span> <span class="ss">:monthly_expenses</span>
</span><span class='line'>  <span class="n">calculates_default_monthly_charge</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>100% may sound extreme, but as soon as we define a single method body in any
one of the models, the declarative nature of the file begins to degrade, and so
does its fitness for the purpose of specification. Plus, if we can only
understand the expected behavior of a model by looking at <em>both</em> its spec and
its implementation, we&rsquo;ve lost some of the power of a test-driven approach.</p>

<h2>What do you think?</h2>

<p>Do you spec associations? If so, what value do you get from doing so? If not,
have you run into situations where you wished you had?</p>

<p>Same questions for validations.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-rails-2.8.1 is released]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/01/04/rspec-rails-281-is-released/"/>
    <updated>2012-01-04T23:43:28-06:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/01/04/rspec-rails-281-is-released</id>
    <content type="html"><![CDATA[<h2>Bug fix release</h2>

<p>The rails-3.2.0.rc2 release broke <code>stub_model</code> in rspec-rails-2.0.0 > 2.8.0.
The rspec-rails-2.8.1 release fixes this issue, but it means that when you
upgrade to rails-3.2.0.rc2 or greater, you&rsquo;ll have to upgrade to
rspec-rails-2.8.1 or greater.</p>

<p>Because rspec-rails-2.8.1 supports all versions of rails since 3.0, I recommend
that you upgrade to rspec-rails-2.8.1 first, and then upgrade to
rails-3.2.0.rc2 (or 3.2.0 once it&rsquo;s out).</p>

<h3>Changelog</h3>

<p><a href="http://rubydoc.info/gems/rspec-rails/file/Changelog.md">http://rubydoc.info/gems/rspec-rails/file/Changelog.md</a></p>

<h3>Docs</h3>

<p><a href="http://rubydoc.info/gems/rspec-rails">http://rubydoc.info/gems/rspec-rails</a></p>

<p><a href="http://relishapp.com/rspec/rspec-rails">http://relishapp.com/rspec/rspec-rails</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[RSpec-2.8 is released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2012/01/04/rspec-28-is-released/"/>
    <updated>2012-01-04T20:38:03-06:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2012/01/04/rspec-28-is-released</id>
    <content type="html"><![CDATA[<p>We released RSpec-2.8.0 today with a host of new features and improvements
since 2.7. Some of the highlights are described below, but you can see the
full changelogs at:</p>

<ul>
<li><a href="http://rubydoc.info/gems/rspec-core/file/Changelog.md">http://rubydoc.info/gems/rspec-core/file/Changelog.md</a></li>
<li><a href="http://rubydoc.info/gems/rspec-expectations/file/Changelog.md">http://rubydoc.info/gems/rspec-expectations/file/Changelog.md</a></li>
<li><a href="http://rubydoc.info/gems/rspec-mocks/file/Changelog.md">http://rubydoc.info/gems/rspec-mocks/file/Changelog.md</a></li>
<li><a href="http://rubydoc.info/gems/rspec-rails/file/Changelog.md">http://rubydoc.info/gems/rspec-rails/file/Changelog.md</a></li>
</ul>


<h2>Documentation</h2>

<p>While not 100% complete yet, we&rsquo;ve made great strides on RSpec&rsquo;s RDoc:</p>

<ul>
<li><a href="http://rubydoc.info/gems/rspec-core">http://rubydoc.info/gems/rspec-core</a></li>
<li><a href="http://rubydoc.info/gems/rspec-expectations">http://rubydoc.info/gems/rspec-expectations</a></li>
<li><a href="http://rubydoc.info/gems/rspec-mocks">http://rubydoc.info/gems/rspec-mocks</a></li>
<li><a href="http://rubydoc.info/gems/rspec-rails">http://rubydoc.info/gems/rspec-rails</a></li>
</ul>


<p><a href="http://rspec.info">http://rspec.info</a> is now just a one pager (desperate for
some design love &ndash; volunteers please email <a href="&#109;&#97;&#x69;&#x6c;&#x74;&#x6f;&#58;&#114;&#115;&#112;&#x65;&#x63;&#45;&#117;&#x73;&#x65;&#114;&#115;&#64;&#114;&#117;&#x62;&#121;&#x66;&#111;&#x72;&#x67;&#101;&#x2e;&#111;&#x72;&#103;">&#114;&#115;&#x70;&#x65;&#x63;&#x2d;&#x75;&#x73;&#x65;&#114;&#115;&#x40;&#114;&#x75;&#x62;&#x79;&#x66;&#x6f;&#x72;&#x67;&#x65;&#x2e;&#111;&#x72;&#x67;</a>). All the
old pages are redirects to the relevant RDoc at <a href="http://rubydoc.info.">http://rubydoc.info.</a> RSpec-1
info is still available at <a href="http://old.rspec.info">http://old.rspec.info</a>.</p>

<p>We&rsquo;ve still got Cucumber features up at
<a href="http://relishapp.com/rspec">http://relishapp.com/rspec</a>, but we&rsquo;re going to be
phasing that out as the primary source of documentation. There are a lot of
reasons for this, and I&rsquo;ll try to follow up with a separate blog post on this
topic.</p>

<h2>rspec-core</h2>

<h3>Improved support for tags and filtering</h3>

<p>You can now set default tags/filters in either <code>RSpec.configure</code> or a <code>.rspec</code>
file and override these tags on the command line. For example, this configuration
tells rspec to run all the examples that are not tagged <code>:slow</code>:</p>

<pre><code># in spec/spec_helper.rb
RSpec.configure do |c|
  c.treat_symbols_as_metadata_keys_with_true_values = true
  c.filter_run_excluding :slow
end
</code></pre>

<p>Now when you want run those, you can just do this:</p>

<pre><code>rspec --tag slow
</code></pre>

<p>This will override the configuration and run onlly the examples tagged <code>:slow</code>.</p>

<h3>&mdash;order rand</h3>

<p>We added an <code>--order</code> option with two supported values: <code>rand</code> and <code>default</code>.</p>

<p><code>rspec --order random</code> (or <code>rand</code>) tells RSpec to run the groups in a random
order, and then run the examples within each group in random order. We
implemented it this way (rather than complete randomization of every example)
because we don&rsquo;t want to re-run expensive before(:all) hooks. A fair tradeoff,
as the resulting randomization is just as effective at exposing
order-dependency bugs.</p>

<p>When you use <code>--order random</code>, RSpec prints out the random number it used to
seed the randomizer. When you think you&rsquo;ve found an order-dependency bug, you
can pass the seed along and the order will remain consistent:</p>

<pre><code>--order rand:3455
</code></pre>

<p><code>--order default</code> tells RSpec to load groups and examples as they are declared
in each file.</p>

<h3>rspec &mdash;init</h3>

<p>We added an <code>--init</code> switch to the <code>rspec</code> command to generate a &ldquo;spec&rdquo;
directory, and  &ldquo;.rspec&rdquo; and &ldquo;spec/spec_helper.rb&rdquo; files with some starter code
in them.</p>

<h2>rspec-expectations</h2>

<p>We discovered that <a href="https://github.com/rspec/rspec-expectations/blob/master/benchmarks/matcher_dsl_vs_classes.rb">the matcher DSL generates matchers that run considerably
slower than classes which implement the matcher
protocol</a>.
We made some minor improvements in the DSL, but to really improve things we
re-implemented every single built-in matcher as a class.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-2.8.0.rc1 is released]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2011/11/06/rspec-280rc1-is-released/"/>
    <updated>2011-11-06T11:02:40-06:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2011/11/06/rspec-280rc1-is-released</id>
    <content type="html"><![CDATA[<p>I just released rspec-2.8.0.rc1, which includes releases of rspec-core,
rspec-expectations, rspec-mocks, and rspec-rails. Changelogs for each are at:</p>

<ul>
<li><a href="https://github.com/rspec/rspec-core/blob/master/Changelog.md">rspec-core</a></li>
<li><a href="https://github.com/rspec/rspec-expectations/blob/master/Changelog.md">rspec-expectations</a></li>
<li><a href="https://github.com/rspec/rspec-mocks/blob/master/Changelog.md">rspec-mocks</a></li>
<li><a href="https://github.com/rspec/rspec-rails/blob/master/Changelog.md">rspec-rails</a></li>
</ul>


<h2>What&rsquo;s new</h2>

<p>Nothing really changed in rspec-rails or rspec-mocks, but rspec-core and
rspec-expectations have both gotten some nice improvements.</p>

<h3>Configuration (rspec-core)</h3>

<p>rspec-core offers a number of configuration options which can be declared on
the command line, in a config file (<code>.rspec</code>, <code>~/.rspec</code>, or custom location),
as well as in an <code>RSpec.configure</code> block (in <code>spec/spec_helper.rb</code> by
convention). Before this release, some options, but not all, could be stored in
config files and then overridden on the command line. The problems were that it
was inconsistent (not all options worked this way), and we couldn&rsquo;t override
options that were set in <code>RSpec.configure</code> blocks.</p>

<p>With this release, almost all options declared in <code>RSpec.configure</code> can be
overridden from the command line, and <code>--tag</code> options can override their
inverses. For example, if you have this in <code>.rspec</code>:</p>

<pre><code>--tag ~slow:true
</code></pre>

<p>That means &ldquo;exclude examples tagged <code>:slow =&gt; true</code>&rdquo;. So the following example
would be excluded:</p>

<pre><code>it "does something", :slow =&gt; true do
  # ...
end
</code></pre>

<p>You can also exclude that example from <code>RSpec.configure</code> like this:</p>

<pre><code>RSpec.configure do |c|
  c.filter_run_excluding :slow =&gt; true
end
</code></pre>

<p>Note: the naming is different for historical reasons, and we will reconcile
that in a future release, but for now, just know that <code>--tag</code> on the command
line and in <code>.rspec</code> is synonymous with <code>filter_run_[including|excluding]</code> in
<code>RSpec.configure</code>.</p>

<h3>Override from command line</h3>

<p>Whether the default is stored in <code>.rspec</code> or <code>RSpec.configure</code>, it can be overridden
from the command line like this:</p>

<pre><code>rspec --tag slow:true
</code></pre>

<h3>&ldquo;Profiles&rdquo; in custom options files</h3>

<p>The <code>rspec</code> command has an <code>--options</code> option that let&rsquo;s store command line args in
arbitrary files and tell RSpec where to find them. For example, you could set things
up so your normal spec run excludes the groups and examples marked <code>:slow</code> by putting
this in <code>.rspec</code>:</p>

<pre><code>--tag ~slow
</code></pre>

<p>Now add a <code>.slow</code> file with:</p>

<pre><code>--tag slow
</code></pre>

<p>Now run <code>rspec</code> to run everything but the slow specs, and run <code>rspec --options
.slow</code> or <code>rspec -O.slow</code> to run the slow ones.</p>

<h3>Override from Rake task</h3>

<p>RSpec&rsquo;s Rake task supports an <code>rspec_opts</code> config option, which means you can
set up different groupings from rake tasks as well. The fast/slow example above
would look like this:</p>

<pre><code>namespace :spec do
  desc "runs the fast specs"
  RSpec::Core::RakeTask.new(:fast) do |t|
    t.rspec_opts = '--options .fast'
  end
  RSpec::Core::RakeTask.new(:slow) do |t|
    t.rspec_opts = '--options .slow'
  end
end
</code></pre>

<p>Or ..</p>

<pre><code>namespace :spec do
  desc "runs the fast specs"
  RSpec::Core::RakeTask.new(:fast) do |t|
    t.rspec_opts = '--tag ~slow'
  end
  RSpec::Core::RakeTask.new(:slow) do |t|
    t.rspec_opts = '--tag slow'
  end
end
</code></pre>

<h3>Implicit <code>true</code> value for tags/filters</h3>

<p>This is not new in rspec-2.8, but all the tags/filters in the example above can
be written without explicitly typing <code>true</code>:</p>

<pre><code>--tag slow
--tag ~slow







RSpec.configure {|c| c.filter_run_excluding :slow}

it "does something", :slow do
</code></pre>

<p>You have to set a config option to enable this in rspec-2.x:</p>

<pre><code>RSpec.configure {|c| c.treat_symbols_as_metadata_keys_with_true_values = true}
</code></pre>

<p>In rspec-3.0, this will be the default, but without setting this value in 2.x
you&rsquo;ll get a deprecation warning when you try to configure things this way.
It&rsquo;s ugly, I know, but this enabled us to introduce the new behavior without
breaking compatibility with some suites in a minor release.</p>

<h3>Ordering</h3>

<p>With 2.8, you can now run the examples in random order, using the new <code>--order</code>
option:</p>

<pre><code>--order rand
</code></pre>

<p>The order is randomized with some reasonable caveats:</p>

<ul>
<li>top level example groups are randomized</li>
<li>nested groups are randomized within their parent group</li>
<li>examples are randomized within their group</li>
</ul>


<p>This provides a very useful level of randomization while maintaining the
integrity of before/after <code>hooks</code>, <code>subject</code>, <code>let</code>, etc.</p>

<p>If you want to run the examples in the default ordering (file-system load
order for files and declaration order for groups/examples), you can override
the order from the command line:</p>

<pre><code>--order default
</code></pre>

<h3>Pseudo-randomization</h3>

<p>The randomization is managed by Ruby&rsquo;s pseudo-randomization. This means that if
you find an order dependency and want to debug/fix it, you can fix the order by
providing the same seed for each run:</p>

<pre><code>--order rand:1234
</code></pre>

<p>The seed is printed to the console with each run, so you can just copy it to the
command. You can also just specify the seed, which RSpec will assume means you want
to run with <code>--order rand</code>:</p>

<pre><code>--seed 1234
</code></pre>

<p>Every time you run the suite with the same seed, the examples will run in the
same &ldquo;random&rdquo; order.</p>

<h3>Built-in matchers are all classes in rspec-expectations</h3>

<p>The <a href="http://rubydoc.info/github/rspec/rspec-expectations/master/RSpec/Matchers">Matcher
DSL</a>
in rspec-expectations makes it dead simple to define custom matchers that suit
your domain. The problem is that it is <a href="https://github.com/rspec/rspec-expectations/blob/master/benchmarks/matcher_dsl_vs_classes.rb">several times slower than defining a
class to do
so</a>.
While this doesn&rsquo;t make much difference when you have a custom matcher that you
use a few dozen times (where talking hundredths of seconds here), it does make
a difference if every single matcher invocation in your entire suite suffers
this problem.</p>

<p>The short term fix is that all of the built-in matchers have been
re-implemented as classes rather than using the DSL to declare them. This has
the added benefit of making it easier to navigate the code and RDoc</p>

<p>Longer term, we&rsquo;ll try to refactor the internals of the matcher DSL so that it
generates a class at declaration time. Eventually.</p>

<h3>Summing up</h3>

<p>So that&rsquo;s it. Nothing ground breaking. Nothing compatibility
breaking. But some nice new features and improvements that will make your life
just a little bit nicer when you upgrade. We&rsquo;re doing a release candidate
because enough changed internally that I want to give you time to try it out,
so please, please do so! And please report any issues you&rsquo;re having with this
upgrade to:</p>

<ul>
<li><a href="https://github.com/rspec/rspec-core/issues">rspec-core</a></li>
<li><a href="https://github.com/rspec/rspec-expectations/issues">rspec-expectations</a></li>
<li><a href="https://github.com/rspec/rspec-mocks/issues">rspec-mocks</a></li>
<li><a href="https://github.com/rspec/rspec-rails/issues">rspec-rails</a></li>
</ul>


<p>Assuming there are no significant issues, I&rsquo;ll release 2.8 final within a week
or two.</p>

<p>Happy spec&#8217;ing!</p>

<p>David</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-core 2.7.1 is released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2011/10/20/rspec-core-271-is-released/"/>
    <updated>2011-10-20T07:13:43-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2011/10/20/rspec-core-271-is-released</id>
    <content type="html"><![CDATA[<h3>rspec-core-2.7.1</h3>

<p><a href="http://github.com/rspec/rspec-core/compare/v2.7.0...v2.7.1">full changelog</a></p>

<ul>
<li>Bug fixes</li>
<li>tell autotest the correct place to find the rspec executable</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-2.7.0 is released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2011/10/16/rspec-270-is-released/"/>
    <updated>2011-10-16T15:28:00-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2011/10/16/rspec-270-is-released</id>
    <content type="html"><![CDATA[<p>We&rsquo;re pleased to announce the release of rspec-2.7.0. Release notes for each
gem are listed below, but here are a couple of highlights:</p>

<h2>Just type <code>rspec</code></h2>

<p>With the the 2.7.0 release, if you keep all of your specs in the conventional
<code>spec</code> directory, you don&rsquo;t need to follow the <code>rspec</code> command with a path.
Just type <code>rspec</code>.</p>

<p>If you keep your specs in a different directory, just set the <code>--default_path</code>
option to that directory on the command line, or in a <code>.rspec</code> config file.</p>

<h2>The rake task now lets Bundler manage Bundler</h2>

<p>The <code>RSpec::Core::RakeTask</code> invokes the <code>rspec</code> command in a subshell. In
recent releases, it assumed that you wanted it prefixed with <code>bundle exec</code> if
it saw a <code>Gemfile</code>. We then added <code>gemfile</code> and <code>skip_bundler</code> options to the
task, so you could manage this in different ways.</p>

<p>It turns out that Bundler manages this quite well without any help from RSpec.
If you activate Bundler in the parent shell, via the command line or
<code>Bundler.setup</code>, it sets environment variables that activate Bundler in the
subshell with the correct gemfile.</p>

<p>The <code>gemfile</code> and <code>skip_bundler</code> options are therefore deprecated and have no
effect.</p>

<h2>Release Notes</h2>

<h3>rspec-core-2.7.0</h3>

<p><a href="http://github.com/rspec/rspec-core/compare/v2.6.4...v2.7.0">full changelog</a></p>

<p>NOTE: RSpec&rsquo;s release policy dictates that there should not be any backward
incompatible changes in minor releases, but we&rsquo;re making an exception to
release a change to how RSpec interacts with other command line tools.</p>

<p>As of 2.7.0, you must explicity <code>require "rspec/autorun"</code> unless you use the
<code>rspec</code> command (which already does this for you).</p>

<ul>
<li><p>Enhancements</p>

<ul>
<li>Add example.exception (David Chelimsky)</li>
<li><code>--default_path</code> command line option (Justin Ko)</li>
<li>support multiple <code>--line_number</code> options (David J. Hamilton)

<ul>
<li>also supports <code>path/to/file.rb:5:9</code> (runs examples on lines 5 and 9)</li>
</ul>
</li>
<li>Allow classes/modules to be used as shared example group identifiers
(Arthur Gunn)</li>
<li>Friendly error message when shared context cannot be found (Sławosz
Sławiński)</li>
<li>Clear formatters when resetting config (John Bintz)</li>
<li>Add <code>xspecify</code> and xexample as temp-pending methods (David Chelimsky)</li>
<li>Add <code>--no-drb</code> option (Iain Hecker)</li>
<li>Provide more accurate run time by registering start time before code
is loaded (David Chelimsky)</li>
<li>Rake task default pattern finds specs in symlinked dirs (Kelly Felkins)</li>
<li>Rake task no longer does anything to invoke bundler since Bundler already
handles it for us. Thanks to Andre Arko for the tip.</li>
<li>Add <code>--failure-exit-code</code> option (Chris Griego)</li>
</ul>
</li>
<li><p>Bug fixes</p>

<ul>
<li>Include <code>Rake::DSL</code> to remove deprecation warnings in Rake > 0.8.7 (Pivotal
Casebook)</li>
<li>Only eval <code>let</code> block once even if it returns <code>nil</code> (Adam Meehan)</li>
<li>Fix <code>--pattern</code> option (wasn&rsquo;t being recognized) (David Chelimsky)</li>
<li>Only implicitly <code>require "rspec/autorun"</code> with the <code>rspec</code> command (David
Chelimsky)</li>
<li>Ensure that rspec&rsquo;s <code>at_exit</code> defines the exit code (Daniel Doubrovkine)</li>
<li>Show the correct snippet in the HTML and TextMate formatters (Brian
Faherty)</li>
</ul>
</li>
</ul>


<h3>rspec-expectations-2.7.0</h3>

<p><a href="http://github.com/rspec/rspec-expectations/compare/v2.6.0...v2.7.0">full changelog</a></p>

<ul>
<li><p>Enhancements</p>

<ul>
<li>HaveMatcher converts argument using <code>to_i</code> (Alex Bepple &amp; Pat Maddox)</li>
<li>Improved failure message for the <code>have_xxx</code> matcher (Myron Marston)</li>
<li>HaveMatcher supports <code>count</code> (Matthew Bellantoni)</li>
<li>Change matcher dups <code>Enumerable</code> before the action, supporting custom
<code>Enumerable</code> types like <code>CollectionProxy</code> in Rails (David Chelimsky)</li>
</ul>
</li>
<li><p>Bug fixes</p>

<ul>
<li>Fix typo in <code>have(n).xyz</code> documentation (Jean Boussier)</li>
<li>fix <code>safe_sort</code> for ruby 1.9.2 (<code>Kernel</code> now defines <code>&lt;=&gt;</code> for Object)
(Peter van Hardenberg)</li>
</ul>
</li>
</ul>


<h3>rspec-mocks-2.7.0</h3>

<p><a href="http://github.com/rspec/rspec-mocks/compare/v2.6.0...v2.7.0">full changelog</a></p>

<ul>
<li><p>Enhancements</p>

<ul>
<li>Use <code>__send__</code> rather than <code>send</code> (alextk)</li>
<li>Add support for <code>any_instance.stub_chain</code> (Sidu Ponnappa)</li>
<li>Add support for <code>any_instance</code> argument matching based on <code>with</code> (Sidu
Ponnappa and Andy Lindeman)</li>
</ul>
</li>
<li><p>Changes</p>

<ul>
<li>Check for <code>failure_message_for_should</code> or <code>failure_message</code> instead of
<code>description</code> to detect a matcher (Tibor Claassen)</li>
</ul>
</li>
<li><p>Bug fixes</p>

<ul>
<li>pass a hash to <code>any_instance.stub</code>. (Justin Ko)</li>
<li>allow <code>to_ary</code> to be called without raising <code>NoMethodError</code> (Mikhail
Dieterle)</li>
<li><code>any_instance</code> properly restores private methods (Sidu Ponnappa)</li>
</ul>
</li>
</ul>


<h3>rspec-rails-2.7.0</h3>

<p><a href="http://github.com/rspec/rspec-rails/compare/v2.6.1...v2.7.0">full changelog</a></p>

<ul>
<li><p>Enhancments</p>

<ul>
<li><code>ActiveRecord::Relation</code> can use the <code>=~</code> matcher (Andy Lindeman)</li>
<li>Make generated controller spec more consistent with regard to ids
(Brent J. Nordquist)</li>
<li>Less restrictive autotest mapping between spec and implementation files
(José Valim)</li>
<li><code>require 'rspec/autorun'</code> from generated <code>spec_helper.rb</code> (David Chelimsky)</li>
<li>add <code>bypass_rescue</code> (Lenny Marks)</li>
<li><code>route_to</code> accepts query string (Marc Weil)</li>
</ul>
</li>
<li><p>Internal</p>

<ul>
<li>Added specs for generators using ammeter (Alex Rothenberg)</li>
</ul>
</li>
<li><p>Bug fixes</p>

<ul>
<li>Fix configuration/integration bug with rails 3.0 (fixed in 3.1) in which
<code>fixure_file_upload</code> reads from <code>ActiveSupport::TestCase.fixture_path</code> and
misses RSpec&rsquo;s configuration (David Chelimsky)</li>
<li>Support nested resource in view spec generator (David Chelimsky)</li>
<li>Define <code>primary_key</code> on class generated by <code>mock_model("WithAString")</code>
(David Chelimsky)</li>
</ul>
</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Avoid stubbing methods invoked by a framework]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2011/09/22/avoid-stubbing-methods-invoked-by-a-framework/"/>
    <updated>2011-09-22T18:23:55-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2011/09/22/avoid-stubbing-methods-invoked-by-a-framework</id>
    <content type="html"><![CDATA[<p>In a <a href="https://github.com/rspec/rspec-mocks/issues/78">github issue</a> reported to
the <a href="https://github.com/rspec/rspec-mocks">rspec-mocks</a> project, the user had
run into a problem in a Rails&#8217; controller spec in which an RSpec-generated test
double didn&rsquo;t behave as expected. What follows is an edited version of the
issue and my response, with the hope that it reaches a wider audience and/or
sparks some conversation.</p>

<h2>The reported problem: ActiveSupport::JSON::Encoding::CircularReferenceError using doubles</h2>

<p>This spec &hellip;</p>

<pre><code>require 'spec_helper'

describe ListsController do
  let(:list) { double("List") }

  describe "GET 'index'" do
    let(:expected) { [{id: "1", name: "test"}] }

    before do
      list.stub(:id){ "1" }
      list.stub(:name){ "test" }
      List.stub(:select){ [ list ] }
    end

    it "should return the list of lists" do
      get :index, format: :json
      response.body.should == expected.to_json
    end
  end
end
</code></pre>

<p>&hellip; plus this implementation &hellip;</p>

<pre><code>class ListsController &lt; ApplicationController
  respond_to :json

  expose(:lists) { List.select("id, name") }

  def index
    respond_with(lists)
  end
end
</code></pre>

<p>&hellip; produces this failure:</p>

<pre><code>  Failure/Error: get :index, format: :json
     ActiveSupport::JSON::Encoding::CircularReferenceError:
       object references itself
</code></pre>

<h2>The deeper problem: this is a great example of when <em>not</em> to use stubs.</h2>

<p>Here&rsquo;s why: there are three incorrect assumptions hiding behind the stubs!</p>

<ol>
<li><code>select</code> takes an Array: <code>List.select(["id","name"])</code>, but the example stubs it incorrectly.</li>
<li>the id is numeric, but the example uses String.</li>
<li>the json is wrapped: <code>{"list":{"id":1,"name":"test"}}</code>, but the example doesn&rsquo;t wrap it.</li>
</ol>


<p>Even if the stubs were properly aligned with reality, the reason for the error
is that <code>respond_with(lists)</code> eventually calls <code>as_json</code> on the <code>list</code> object,
which, in this example, is an RSpec double that doesn&rsquo;t implement <code>as_json</code>.
We need to either use a <code>stub_model</code> (which does implement <code>as_json</code>), or
explicitly stub it in the example:</p>

<pre><code>list.stub(:as_json) { { list: {id: 1, name: "test"} } }
</code></pre>

<p>But I&rsquo;d avoid stubs altogether in this case. Stubs are great for well defined
(and understood) public APIs <em>which are invoked by the code being specified</em>.
In this case, we&rsquo;re stubbing an API (<code>as_json</code>) that is invoked by the Rails
framework, not the code being specified. If the Rails framework ever changes
how it renders json, this example would continue to pass, but it would be a
false positive.</p>

<h2>One possible remedy</h2>

<h4>Here&rsquo;s how I&rsquo;d approach this outside-in (based on my own flow, design preferences, and target outcomes. YMMV.)</h4>

<p>Start with a request spec:</p>

<pre><code>require 'spec_helper'

describe "Lists" do
  describe "GET 'index.json'" do
    it "returns the list of lists" do
      list = List.create!(name: "test")
      get "/lists.json"
      response.body.should == [{list: {id: list.id, name: "test"}}].to_json
    end
  end
end
</code></pre>

<p>This shows exactly what to expect, so when working on clients we can refer
directly to this without having to dig into internals.</p>

<p>Run this and it fails with <code>uninitialized constant List</code>, so generate the list
resource:</p>

<pre><code>rails generate resource list name:string
rake db:migrate
rake db:test:prepare
</code></pre>

<p>Run it again and it fails with <code>ActionView::MissingTemplate</code>. Now we have a
couple of choices. The purist view says &ldquo;write a controller spec&rdquo;, but some
people say controller specs are unnecessary if there are already request specs
(or cukes) as they just add duplication.</p>

<p>For me, the answer depends upon the complexity of the requirement as it
compares to what we get for free from Rails. In this case, the only difference
between the requirement and what Rails gives us for free is that we constrain
the fields to <code>id</code> and <code>name</code> This is something we can implement in the model,
so I&rsquo;d just implement this very simple controller code and move on:</p>

<pre><code>class ListsController &lt; ApplicationController
  respond_to :json

  def index
    respond_with List.all
  end
end
</code></pre>

<p>Now the request spec fails with:</p>

<pre><code>expected: "[{\"list\":{\"id\":1,\"name\":\"test\"}}]"
     got: "[{\"list\":{\"created_at\":\"2011-08-27T14:56:19Z\",\"id\":1,\"name\":\"test\",\"updated_at\":\"2011-08-27T14:56:19Z\"}}]"
</code></pre>

<p>We&rsquo;re getting more key/value pairs than we want. I want the model responsible
for constraining the keys in the json (Rails implements json transformations in
the context of the model, so why shouldn&rsquo;t we?), so I&rsquo;d add a model spec:</p>

<pre><code>require 'spec_helper'

describe List do
  describe "#as_json" do
    it "constrains keys to id and name" do
      list = List.new(:name =&gt; "things")
      list.as_json['list'].keys.should eq(%w[id name])
    end
  end
end
</code></pre>

<p>This fails with:</p>

<pre><code>expected ["id", "name"]
     got ["created_at", "name", "updated_at"]
</code></pre>

<p>I expect to see <code>created_at</code> and <code>updated_at</code>, but I&rsquo;m surprised (initially) to
see that <code>id</code> is missing. Thinking this through, it makes sense because the
example generates the <code>list</code> using <code>new</code>, so no <code>id</code> is generated.  To get <code>id</code>
to show up in the list of keys, we can use <code>create</code> instead of <code>new</code>, or we can
explicitly set it. I&rsquo;m going to go with setting the id explicitly to avoid the
db hit, accepting the self-imposed leaky abstraction. It&rsquo;s all trade-offs.</p>

<pre><code>it "constrains fields to id and name" do
  list = List.new(:name =&gt; "things")
  list.id = 37
  list.as_json['list'].keys.should eq(%w[id name])
end
</code></pre>

<p>Now it fails with:</p>

<pre><code>expected ["id", "name"]
     got ["created_at", "id", "name", "updated_at"]
</code></pre>

<p>Now we can implement the constraint:</p>

<pre><code>class List &lt; ActiveRecord::Base
  def as_json
    super({ only: %w[id name]})
  end
end
</code></pre>

<p>Now the model spec passes, but the request spec fails with:</p>

<pre><code>ArgumentError:
  wrong number of arguments (1 for 0)
</code></pre>

<p>This is because the <code>as_json</code> implementation fails to honor the <a href="http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json">Rails
API</a>:</p>

<pre><code>as_json(options = nil)
</code></pre>

<p><code>as_json</code> is called by the Rails framework with an options hash. Had we done
this without the request spec and weren&rsquo;t aware of this information, we&rsquo;d have
a bunch of passing specs but the app would blow up. Hooray for testing at
multiple levels!</p>

<p>So we add a new example to the model spec:</p>

<pre><code>it "honors the submitted options hash" do
  list = List.new(:name =&gt; "things")
  list.id = 37
  list.as_json(:only =&gt; :name)['list'].keys.should eq(%w[name])
end
</code></pre>

<p>This fails with <code>wrong number of arguments (1 for 0)</code> as well, so now we adjust
the model implementation:</p>

<pre><code>def as_json(opts={})
  super({ only: %w[id name]}.merge(opts))
end
</code></pre>

<p>Now the model spec passes again, and so does the request spec! DONE!</p>

<p>The result is a very nice balance of clarity, speed (in spite of the one db hit
in the request spec) and flexibility. Any new endpoints we add will get the
same json representation because it is expressed in the model (heeding the
principle of least surprise). The model spec not only specifies how the model
should represent itself as json, but it helps to explain how the rails
framework uses the model. All of this with no stubbing at all, and especially
no stubbing of APIs our code isn&rsquo;t invoking.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Stop typing "bundle exec"]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2011/07/18/stop-typing-bundle-exec/"/>
    <updated>2011-07-18T08:15:26-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2011/07/18/stop-typing-bundle-exec</id>
    <content type="html"><![CDATA[<p><a href="http://gembundler.com/">Bundler</a> serves two primary purposes:</p>

<ol>
<li>it helps you to install the correct gem versions</li>
<li>it constrains the load path to the correct gem versions at runtime</li>
</ol>


<p>Assuming you&rsquo;re using Bundler to constrain your runtime environment (which you
are if you&rsquo;re using Rails 3 defaults), then you are likely prefixing most shell
commands with <code>bundle exec</code>.</p>

<h3>We interrupt this post for an important update:</h3>

<p>Two important pieces of information in the comments:</p>

<ol>
<li>Prepending <code>./bin</code> to your path exposes a serious security risk. Proceed with caution.</li>
<li><a href="http://beginrescueend.com/integration/bundler/">rvm >= 1.6.18 + bundler >= 1.0.5 removes the need for this altogether</a>.</li>
</ol>


<h3>We now return you to our regularly scheduled post:</h3>

<p>Here&rsquo;s a little tip to help save you the prefix, without adding any aliases or
functions to your environment.</p>

<pre><code>bundle install --binstubs
export PATH=./bin:$PATH
</code></pre>

<p><code>bundle install --binstubs</code> creates a <code>bin</code> directory at the root of your
project, and fills it with Bundler-enabled wrappers for all of the executables
installed by the gems listed in your Gemfile. This enables you to type
<code>bin/rake</code> instead of <code>bundle exec rake</code>, for example, ensuring that the
correct version of rake is loaded.</p>

<p>Now prepend <code>./bin</code> to your path and you can just type <code>rake</code>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-core-2.6.4 is released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2011/06/06/rspec-core-264-is-released/"/>
    <updated>2011-06-06T06:47:22-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2011/06/06/rspec-core-264-is-released</id>
    <content type="html"><![CDATA[<p><a href="http://github.com/rspec/rspec-core/compare/v2.6.3...v2.6.4">full changelog</a></p>

<ul>
<li>Bug fixes

<ul>
<li>Support exclusion filters in DRb. (Yann Lugrin)</li>
<li>Fix &mdash;example escaping when run over DRb. (Elliot Winkler)</li>
<li>Use standard ANSI codes for color formatting so colors work in a wider set
of color schemes.</li>
</ul>
</li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rake 0.9 and gem version constraints]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2011/05/28/rake-09-and-gem-version-constraints/"/>
    <updated>2011-05-28T17:03:57-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2011/05/28/rake-09-and-gem-version-constraints</id>
    <content type="html"><![CDATA[<p>There&rsquo;s been some confusion surrounding the rake-0.9.0 release, and I&rsquo;d like to
take the opportunity to clarify some things and hopefully draw attention to gem
versioning policies and their implications for everybody in the Ruby community.</p>

<p>First, there are three distinct issues related to the rake release:</p>

<h3>1. Backward-incompatibility</h3>

<p>Rake 0.9 includes backward-incompatible changes. Per the
<a href="https://github.com/jimweirich/rake/blob/master/CHANGES">changelog</a>:</p>

<pre><code>## Version 0.9.0

* *Incompatible* *change*: Rake DSL commands ('task', 'file', etc.)  are
  no longer private methods in Object.  If you need to call 'task :xzy' inside
  your class, include Rake::DSL into the class.  The DSL is still available at
  the top level scope (via the top level object which extends Rake::DSL).
</code></pre>

<p>This conflicts with the way Rails, among others, uses Rake, among others.  The
workaround recommended by <a href="https://twitter.com/dhh">@dhh</a> is to constrain the
rake version in the Gemfiles in your Rails applications:</p>

<pre><code>gem "rake", "0.8.7"
</code></pre>

<p>This is a perfectly fine short term solution to keep your applications running,
but it won&rsquo;t be long before a gem that your Rails application depends on,
either directly or through the transitive property of dependencies, is going to
specify any of:</p>

<pre><code>gemspec.add_dependency 'rake', '0.9.0'
gemspec.add_dependency 'rake', '&gt;= 0.9.0'
gemspec.add_dependency 'rake', '~&gt; 0.9.0'
</code></pre>

<p>When that happens, you&rsquo;ll need to loosen the constraint in your app if you want
to upgrade any of the gems downstream from the gem that introduces this
dependency. This is not a big deal because you can control the situation
directly in your own Gemfile in your own application.</p>

<h3>Libraries are not applications</h3>

<p>This advice should <em>not</em>, however, be applied to any gems that depend on Rake.
Let&rsquo;s say you&rsquo;re using two gems that both provide Rake tasks and therefore
depend on the rake gem. At some point the maintainer of gem aaa changes the
constraint to <code>"= 0.8.7"</code>, and the maintainer of gem bbb keeps a looser
constraint: either <code>"&gt;= 0.8.7"</code> or <code>"~&gt; 0.8.7"</code>. You upgrade to the new version
of aaa and everything is fine because both constraints are satisfied by
rake-0.8.7.</p>

<p>A little while down the road, the constraint in bbb changes to <code>"~&gt; 0.9.0"</code>. At
this point you are unable to have the newest versions of aaa and bbb in the
same application. This may not seem like a big deal because you can choose to
not upgrade bbb at this point, but the further upstream the dependency (i.e.
aaa depends on bbb, which depends on ccc), the more likely you are to be
constrained in your upgrade choices.</p>

<p>In short, if you are maintaining a gem that applications or other gems depend
on, you are doing end users a disservice by locking down any upstream
dependency at one and only one version number.</p>

<p>Now here&rsquo;s the catch: while some gem maintainers follow some sort of standard
versioning and/or release policy, there are many that don&rsquo;t.  If you put in a
looser version constraint on a gem whose maintainers introduce breaking changes
in patch releases, you are also doing your users a disservice. More on this
later.</p>

<h3>2. Rake is used to run tasks that depend on Rake</h3>

<p>Perhaps you&rsquo;ve run into this interaction (or similar):</p>

<pre><code>$ bundle install
$ rake db:migrate
You have already activated rake 0.9.0, but your Gemfile requires rake 0.8.7. Consider using bundle exec.
</code></pre>

<p>In this case, the application has an explicit dependency on rake-0.8.7, but
rake-0.9.0 is installed in the shell environment. When you type <code>rake xxx</code>,
Rubygems activates the 0.9.0 version (the newest version installed), and then
tries to activate 0.8.7 when the app is running.</p>

<p>This is a catch 22 that we&rsquo;ve been lulled into ignoring by the mere fact that
there have not been any rake releases for a couple of years (rake-0.9.0 was
released 2 years and 5 days after rake-0.8.7). We all expect to type <code>rake xxx</code>
and have it just work. Why not? It&rsquo;s worked thus far, right?</p>

<p>During the two years of rake-0.8.7, Bundler was born. You may remember that the
Bundler team took a lot of heat during its early days. One of the complaints I
remember was that people didn&rsquo;t want to have to type <code>bundle exec</code> to run a
rake task. The result is that pretty much all apps that use Bundler and Rake
have this in their Rakefiles:</p>

<pre><code>require 'rubygems'
require 'bundler'
Bundler.setup
</code></pre>

<p>This enables us to type <code>rake xxx</code> and let Bundler manage loading every other
gem besides rake, which is already loaded by Rubygems. So now when we find both
rake-0.8.7 and 0.9.0 in our gem environment, and the app we&rsquo;re working with depends
explicitly on 0.8.7, we have (at least) three options:</p>

<h4>a. Tell bundler to install the rake command PROJECT_ROOT/bin</h4>

<pre><code>bundle install --binstubs
</code></pre>

<p>Now you can run this</p>

<pre><code>bin/rake xxx
</code></pre>

<h4>b. Explicitly run bundle exec</h4>

<pre><code>bundle exec rake
</code></pre>

<p>In either of the first two options, Bundler controls the activation of the rake
gem for you, which allows it to put the correct version on the <code>$LOAD_PATH</code>.</p>

<h4>c. Just remove 0.9 from the current gem environment</h4>

<pre><code>gem uninstall rake
</code></pre>

<p>This only works if you&rsquo;re using an isolated gemset for the current project
(e.g. using rvm) or you simply don&rsquo;t need rake-0.9.0 on your system. It also is
not a very reliable way to deal with this if you have any sort of automated
build or deployment system that is installing gems into a shared gem
environment on the build or production servers.</p>

<p>The real problem here is not that we have to type a different command on the
command line. We humans can adapt and get used to doing that. The deeper
problem is that there are countless automation scripts out in the wild that
depend on <code>rake xxx</code>. In order to support both versions of Rake, they will all
have to be changed to use one of the first two solutions noted above. The cost
of this is no small chunk of change, but it is nobody&rsquo;s fault but our own for
failing to recognize the cyclical nature of using a versioned tool to run
applications that might require a different version.</p>

<h3>3. Not all gems expose their dependencies in a way that Bundler or Rubygems can control them</h3>

<p>On my team at DRW, we tried to constrain our rake dependencies to 0.8.7 as a
temporary measure, but each time we installed into a new gem environment we
found that rake-0.9.0 was being installed. It turned out that a gem we depended
on was installing rake through a back door, and with no version constraint at
all.  The result was that neither Bundler nor Rubygems had any control over
this installation relative to our application (Bundler told Rubygems to install
this gem, and this gem silently installed rake). And, to make things more
confusing, Bundler reported that it was installing rake-0.8.7 and said nothing
about 0.9.0.</p>

<p>The maintainer of that gem released new versions right away, so that issue is now
resolved, but it&rsquo;s entirely possible that other gems you&rsquo;re using are doing the
same (or similar). Just something to keep your eye out for.</p>

<h3>What can we learn from all of this?</h3>

<p>One issue this exposes is a lack of common understanding and agreement about
how to manage releases and dependencies. The <a href="http://docs.rubygems.org/read/chapter/7">Rubygems Rational
Versioning</a> policy and
<a href="http://semver.org">Semantic Versioning</a> are both very sound approaches that share a common
scheme for version numbers:</p>

<p>A version has three parts: major, minor, and patch.  For example, release 3.0.0
is a major release because the first number was incremented from 2 to 3, 3.2.0
is a minor release because the second number was incremented from 1 to 2, and
3.2.1 is a patch release because the third number was incremented from 0 to 1.
Both specs state the following:</p>

<ol>
<li>Patch releases (3.2.1) should only include bug fixes and internal implementation changes.</li>
<li>Minor releases (3.2.0) can include bug fixes, internal changes, and new features, but no breaking changes.</li>
<li>Major releases (3.0.0) can include bug fixes, internal changes, new features, and breaking changes.</li>
</ol>


<p>If everybody adhered to either policy, we&rsquo;d all be able to declare our gem dependencies like this:</p>

<pre><code>spec.add_dependency "foo", "&gt;= 2.3", "&lt; 3.0"
</code></pre>

<p>&hellip; or the following, oft misunderstood, shortcut for same:</p>

<pre><code>spec.add_dependency "foo", "~&gt; 2.3"
</code></pre>

<p>This tells Rubygems to install the newest version that is >= 2.3.0, trusting
that no version 2.y.x will include breaking changes.</p>

<h3>RSpec</h3>

<p>I&rsquo;ll confess that I didn&rsquo;t adhere to either approach with RSpec until the
rspec-2.0.0 release, last October. I knowingly introduced breaking changes in
the 1.x series and RSpec likely lost the confidence of a fair sum of users
during that time.</p>

<p>The good news, vis a vis RSpec, is that we&rsquo;ve been following Rubygems Rational
Versioning since the rspec-2.0 release.  While we&rsquo;ve had a couple of
regressions in the process (followed swiftly by patch releases that addressed
them), there has been only one intentionally breaking change, and that was
related to integration with another library. That change was announced,
documented, and I don&rsquo;t recall seeing any issues reported related to it.</p>

<p>We&rsquo;re not doing SemVer yet because it is more strict than RRV, and RSpec does
not currently meet all of its criteria. I do hope, however, to have RSpec on
SemVer before the year is out.</p>

<h3>This all sounds great, but &hellip;</h3>

<p>&hellip; the reality is that getting every gem developer to commit to RRV or SemVer
is very unlikely. What those of us who do <em>can</em> do, however, is try to provide
a balance of flexibility and safety when we declare upstream dependencies. The
<a href="https://rubygems.org/gems/rspec-expectations">rspec-expectations</a> gem, for
example, declares the following runtime dependency:</p>

<pre><code>diff-lcs ~&gt; 1.1.2
</code></pre>

<p>This expresses an opinion that it is safe for your application (that depends on
rspec-expectations) to depend on any 1.1.x version of diff-lcs greater than or
equal to 1.1.2, but it is not safe to depend on 1.2.0.  While this provides a
high degree of safety, it also provides low flexibility: if any other gem your
app depends on depends on diff-lcs-1.2 in the future (not likely, since 1.1.2
was released in 2004, but that&rsquo;s besides the point), you won&rsquo;t be able to use
it with the current release of rspec-expectations, even if the diff-lcs-2.2
release does not include any breaking changes.</p>

<p>If diff-lcs was still under regular maintenance, and it&rsquo;s maintainers were
committed to RRV or SemVer, then rspec-expectations would be able to use this
dependency instead:</p>

<pre><code>diff-lcs ~&gt; 1.2
</code></pre>

<p>This would provide significantly more flexibility in rspec-expectations&rsquo;s
ability to play nice with other gems that also depend on diff-lcs in the same
applciation over a longer period of time.</p>

<p>Note that every gem page on <a href="http://rubygems.org">rubygems.org</a> now includes a
recommendation to use the pessamistic constraint using a three-part version
number (e.g. <code>rake ~&gt; 0.9.0</code>). As just discussed, this provides safety, but
lacks long term flexibility.</p>

<h3>Depending on rake</h3>

<p>So what should maintainers of gems that depend on rake do now? The likelihood
is that some end users will constrain their applications to rake <code>0.8.7</code>, and
others will constrain them to <code>= 0.9.0</code>, <code>~&gt; 0.9.0</code>, or <code>&gt;= 0.9.0</code>. Unless Jim
Weirich announces that rake will follow RRV or SemVer, we have to allow for the
possibility that rake <code>0.10.0</code> will introduce new breaking changes. In this
case, I think the responsible thing to do is make sure our gems work with both
rake-0.8 and 0.9, and specify the dependency like this:</p>

<pre><code>spec.add_runtime_dependency 'rake', '&gt;= 0.8.7', '&lt; 0.10'
</code></pre>

<p>Trusting that no rake 0.9.x version will introduce breaking changes, this
provides the greatest flexibility to end users without exposing them to the
risk of breaking changes in rake-0.10.0.</p>

<h3>Feedback</h3>

<p>I&rsquo;m curious to hear what you think about all of this. Do you think this all
makes sense?  Do you think I&rsquo;m over or understating the importance, complexity,
or severity of these issues?  Do you have a different approach to recommend in
moving forward?  I look forward to your feedback.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[rspec-rails-2.6.1 is released!]]></title>
    <link href="http://blog.davidchelimsky.net/blog/2011/05/25/rspec-rails-261-is-released/"/>
    <updated>2011-05-25T21:24:04-05:00</updated>
    <id>http://blog.davidchelimsky.net/blog/2011/05/25/rspec-rails-261-is-released</id>
    <content type="html"><![CDATA[<p>This is a bug fix release that is compatible with the rails-3.0.0 to 3.0.7, 3.0.8.rc1, and 3.1.0.rc1 (it is mostly, but not fully compatible with but not rails-3.1.0.beta1).</p>

<h3>rspec-rails-2.6.1 / 2011-05-25</h3>

<p><a href="http://github.com/rspec/rspec-rails/compare/v2.6.0...v2.6.1">full changelog</a></p>

<ul>
<li>Bug fixes

<ul>
<li>fix controller specs with anonymous controllers with around filters</li>
<li>exclude spec directory from rcov metrics (Rodrigo Navarro)</li>
<li>guard against calling prerequisites on nil default rake task (Jack Dempsey)</li>
</ul>
</li>
</ul>

]]></content>
  </entry>
  
</feed>
