<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>David Chelimsky: rspec.should use_a_little_less_magic</title>
    <link>http://blog.davidchelimsky.net/articles/2007/01/10/rspec-should-use_a_little_less_magic</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>on software in process and practice</description>
    <item>
      <title>rspec.should use_a_little_less_magic</title>
      <description>&lt;p&gt;&lt;span style="color:red;"&gt;Updated on 21 Jan&lt;/span&gt;&lt;/p&gt;


	&lt;p&gt;One of the great things about Ruby is that it allows common folk like you and me to perform all sorts of magic that would dazzle and amaze the more statically typed of our neighbors.&lt;/p&gt;


	&lt;p&gt;One of the terrible things about Ruby is that, as the secret gets out, the various magic potions we mix begin to conflict with each other, creating the foul stench of bugs that don&amp;#8217;t reveal themselves until someone &lt;span class="caps"&gt;ELSE&lt;/span&gt; does some of their own magic.&lt;/p&gt;


	&lt;p&gt;The most visible conflict has been between RSpec and Ruby on Rails, both of which usurp method_missing to support some devilishly sexy run-time method creation. The problem comes when RoR follows good guidelines like late-binding, and adds its version of method_missing later in the stack than RSpec does, rendering RSpec&amp;#8217;s use of method_missing a bit futile, forcing RSpec to monkey patch Rails in order to work.&lt;/p&gt;


	&lt;p&gt;Well, this conflict may soon see its end.&lt;/p&gt;&lt;p&gt;We&amp;#8217;ve just added support for a new way of setting expectations in RSpec that should support all of the existing expectations &lt;span class="caps"&gt;AND&lt;/span&gt; make it very easy to add custom expectations with no method_missing magic or monkey patching. Thanks to Dan North for this )&lt;strong&gt;&amp;#38;)(&lt;/strong&gt;&amp;#38;ing brilliant idea.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s how it looks:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
result.should equal(3)
light.should be_red
newspaper.should be_read
rspec.should use_a_little_less_magic
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;And here&amp;#8217;s how simple it is to add a custom expectation:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
module HotelExpecations
  class BeBookedSolidOn
    def initialize(date)
      @date = date
    end

    def matches?(hotel)
      hotel.booked_solid_on?(@date)
    end

    def failure_message
      "expected hotel to be booked solid on #{@date}" 
    end

    def negative_failure_message
      "expected hotel to not be booked solid on #{@date}" 
    end
  end

  def be_booked_solid_on(date)
    BeBookedSolidOn.new(date)
  end
end

context "Hotel behaviour" do
  include HotelExpectations

  specify "should be considered booked solid with 100 reservations" do
    #given
    @hotel = Hotel.new

    #when
    (1..100).each { @hotel.reserve_room_for(2).on("12/31/2007")

    #then
    @hotel.should be_booked_solid_on("12/31/2007")
  end

  specify "should NOT be considered booked solid with 99 reservations" do
    #given
    @hotel = Hotel.new

    #when
    (1..99).each { @hotel.reserve_room_for(2).on("12/31/2007")

    #then
    @hotel.should_not be_booked_solid_on("12/31/2007")
  end
end
&lt;/code&gt;
&lt;/pre&gt;

	&lt;p&gt;How easy is that?&lt;img src="?" alt="" /&gt;?!&lt;/p&gt;


	&lt;p&gt;As of this writing, this is only available in the trunk, however it will be quietly released in 0.7.6, and perhaps with a bit more fanfare in 0.8. Those of you who make a habit of using the latest and greatest can do this &lt;span class="caps"&gt;RIGHT NOW&lt;/span&gt;, though beware that until the fanfare is sounded, the details may change a bit.&lt;/p&gt;</description>
      <pubDate>Wed, 10 Jan 2007 06:12:00 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:db906366-cdc3-4515-bdaa-0522d443d12a</guid>
      <author>David</author>
      <link>http://blog.davidchelimsky.net/articles/2007/01/10/rspec-should-use_a_little_less_magic</link>
      <category>rspec</category>
      <category>ruby</category>
      <category>bdd</category>
      <category>rails</category>
    </item>
    <item>
      <title>"rspec.should use_a_little_less_magic" by undees</title>
      <description>&lt;p&gt;Hi, David.&lt;/p&gt;


	&lt;p&gt;Thanks for the explanation.  I&amp;#8217;ll just post a quick response here, &amp;#8216;cause I don&amp;#8217;t think the scope of my answer is substantial enough for the mailing list.&lt;/p&gt;


	&lt;p&gt;I agree that, in general, underscores are more readable than dots.  The question becomes much fuzzier when it&amp;#8217;s underscores with parentheses vs. dots without.  I actually liked your second example slightly better, but I don&amp;#8217;t think my mild preference should be enough to undo all that hard work that&amp;#8217;s gone into trunk.&lt;/p&gt;</description>
      <pubDate>Mon, 05 Feb 2007 04:07:32 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:24e31148-9ce0-4a15-b885-d5ca98871446</guid>
      <link>http://blog.davidchelimsky.net/articles/2007/01/10/rspec-should-use_a_little_less_magic#comment-35</link>
    </item>
    <item>
      <title>"rspec.should use_a_little_less_magic" by David Chelimsky</title>
      <description>&lt;p&gt;Devil indeed.&lt;/p&gt;


	&lt;p&gt;Way back when there was a big dot vs underscore debate and consensus was that underscores were more desirable. What you propose could work, but at this point we&amp;#8217;re fairly well committed to the current path (all of the new code is already implemented).&lt;/p&gt;


	&lt;p&gt;Personally, I prefer it the way it is over all the dots. Here are some examples:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;obj.should be_a_kind_of(SomeType)
hash.should have_key(:key)
result.should be &amp;lt; 7
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;vs&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;obj.should.be_a_kind_of SomeType
hash.should.have_key :key
result.should.be &amp;lt; 7
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;To my eyes, the space w/ parens is easier to read than the dots without parens.&lt;/p&gt;


	&lt;p&gt;If you feel strongly about it though, now is the time to voice your opinion because once we do this release there is no going back. Do so on the mailing lists though.&lt;/p&gt;


	&lt;p&gt;Cheers,
David&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 22:34:27 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:6ed63305-f5ba-4873-a9a2-3a9cfd096ccf</guid>
      <link>http://blog.davidchelimsky.net/articles/2007/01/10/rspec-should-use_a_little_less_magic#comment-34</link>
    </item>
    <item>
      <title>"rspec.should use_a_little_less_magic" by undees</title>
      <description>&lt;p&gt;I like the idea of modding the syntax to reduce/eliminate usage of method_missing, so that RSpec doesn&amp;#8217;t step on other frameworks.&lt;/p&gt;


	&lt;p&gt;Just a devil&amp;#8217;s advocate question here: is there a reason the library isn&amp;#8217;t going with dot-notation instead?&lt;/p&gt;


	&lt;p&gt;the_result.should.equal 5&lt;/p&gt;


	&lt;p&gt;wouldn&amp;#8217;t require all those ((()))s, and we wouldn&amp;#8217;t be stuck with that brace vs. do/end priority puzzle.&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 14:27:45 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:32ddf43a-787e-41e9-8e7b-917a03c9035c</guid>
      <link>http://blog.davidchelimsky.net/articles/2007/01/10/rspec-should-use_a_little_less_magic#comment-33</link>
    </item>
    <item>
      <title>"rspec.should use_a_little_less_magic" by David Chelimsky</title>
      <description>&lt;p&gt;Yes, it should, although the plugin still has a few problems that are going to be a challenge to solve, and they won&amp;#8217;t all be solved w/ the 0.8 release.&lt;/p&gt;


	&lt;p&gt;That said, this will be the last major syntax change for rspec before 1.0, and it will come w/ a translator for people who already use rspec pre-0.8.&lt;/p&gt;</description>
      <pubDate>Thu, 18 Jan 2007 09:27:54 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:e3bd5aa7-7fbc-4cd6-a442-0c4b8988fd6a</guid>
      <link>http://blog.davidchelimsky.net/articles/2007/01/10/rspec-should-use_a_little_less_magic#comment-26</link>
    </item>
    <item>
      <title>"rspec.should use_a_little_less_magic" by weepy</title>
      <description>&lt;p&gt;great !&amp;#8212;does this mean that the Rails integration with Rspec 0.8 will be much smoother ?&lt;/p&gt;</description>
      <pubDate>Thu, 18 Jan 2007 06:25:35 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:3b840651-be6b-4506-ae41-22e5cdf6bf6f</guid>
      <link>http://blog.davidchelimsky.net/articles/2007/01/10/rspec-should-use_a_little_less_magic#comment-25</link>
    </item>
    <item>
      <title>"rspec.should use_a_little_less_magic" by Dan North</title>
      <description>&lt;p&gt;I&amp;#8217;m really pleased to see rspec adopting this convention, but I can&amp;#8217;t claim to have originated the idea.&lt;/p&gt;


	&lt;p&gt;I was first introduced to it by &lt;a href="http://joe.truemesh.com/blog//000511.html"&gt;Joe Walnes&lt;/a&gt; a couple of years ago and I&amp;#8217;ve been using it ever since.&lt;/p&gt;


	&lt;p&gt;I found it led to much more readable assertions. It&amp;#8217;s baked into &lt;a href="http://jbehave.org"&gt;JBehave&lt;/a&gt; as &lt;code&gt;ensureThat()&lt;/code&gt;. Long live convergence!&lt;/p&gt;</description>
      <pubDate>Sun, 14 Jan 2007 19:15:30 -0500</pubDate>
      <guid isPermaLink="false">urn:uuid:33658022-da50-41f6-a897-1518f8a95c87</guid>
      <link>http://blog.davidchelimsky.net/articles/2007/01/10/rspec-should-use_a_little_less_magic#comment-18</link>
    </item>
  </channel>
</rss>
