RSpec-1.1.4

May 27th, 2008

We released RSpec-1.1.4 today. It’s mostly a maintenance release but there are a few of cool new features that you may want to know about and take advantage of.

<h3>hash_including</h3>


<p>One thing that has always been a drag is having to specify every key/value pair in a hash that is received as an argument. This is especially painful in Rails controller examples because Rails adds some data to the hash and the examples really don&#8217;t care about that extra data.</p>


<p>Enter <code>hash_including()</code>.</p>


<p>This is a mock argument matcher that let&#8217;s you expect a hash including certain key/value pairs regardless of anything else that shows up in the hash. So instead of:</p>

account.should_receive(:deposit).with({:amount => 37.42, :date => anything()})

you can just say:

account.should_receive(:deposit).with(hash_including(:amount => 37.42))

and keep the example focused on what you’re really interested in

<p>Thanks to <a href="http://talklikeaduck.denhaven2.com/">Rick DeNatale</a> who submitted this feature request and the patch to implement it.</p>


<h3>The heckler returns</h3>


<p>RSpec wasn&#8217;t correctly supporting heckle for a while but the spec-heckler is back in action. For those unfamiliar, you can read about heckle at <a href="http://blog.zenspider.com/2007/06/heckle-version-141-has-been-re.html">zenspider&#8217;s blog</a>.</p>


<p>Here&#8217;s how you heckle your Animal model in your PetStore app:</p>

spec spec/models/animal_spec.rb --heckle Animal

Thanks to Antti Tarvainen for resurrecting this one.

<h3>stub_model</h3>


<p>This is for rails developers who like writing view examples with <code>mock_model()</code> but are sick and tired of having to stub every single attribute that gets referenced in a view.</p>


<p>Instead of creating a mock object like <code>mock_model()</code> does, <code>stub_model()</code> creates an instance of a real model class, but cuts off it&#8217;s connection to the database, raising an error any time it tries to connect to the database.</p>


<p>This is inspired by projects like <a href="http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database">unit_record</a> and <a href="http://agilewebdevelopment.com/plugins/nulldb">NullDB</a>, but let&#8217;s you do things at a more granular level &#8211; allowing you to hit the db in some cases (where you think you really need it) and not in others.</p>


<p>Of course, you may prefer to the sort of &#8220;protection&#8221; you get from those projects, which ensure that no code touches the DB at all. If you do, have at it. This is just another option for you.</p>


<h3>All this and more</h3>


<p>These are just a few of the issues addressed in 1.1.4. For more information, check out the <a href="http://rspec.info/changes.html">changelog</a> and <a href="http://rspec.lighthouseapp.com/projects/5645">lighthouse</a>.</p>

6 Responses to “RSpec-1.1.4”

  1. thilo Says:

    Thanks for the great piece of software, it made my life a lot easier. I just noticed that I don’t get the current 1.1.4 Version via svn when doing a svn update with the externals pointing to the CURRENT tag.

  2. Thomas Maurer Says:

    its on github.com now.

  3. Luke Redpath Says:

    Congrats on the release. FYI, if you’re using Mocha for your mocking/stubbing, it has the equivalent functinoality to hash_including but with a different API:

    
    object = mock()
    object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2))
    object.method_1('key_1' => 1, 'key_2' => 2, 'key_3' => 3)

    no error raised

    object = mock() object.expects(:method_1).with(has_entries('key_1' => 1, 'key_2' => 2)) object.method_1('key_1' => 1, 'key_2' => 99)

    error raised, because method_1 was not called with Hash containing entries: 'key_1' => 1, 'key_2' => 2

    It also has has_entry, has_key and has_value.

    <p><a href="http://mocha.rubyforge.org/classes/Mocha/ParameterMatchers.html">Mocha Parameter Matchers RDoc</a></p>
    
  4. Foo Bar Says:

    The problem is that the rspec.info site doesn’t yet have update installation instructions.

  5. Collin VanDyck Says:

    Thank you! I am very excited about stub_model, as we have also experienced the excessive stubbing in our view specs. Keep up the good work!

  6. Brian Takita Says:

    Theres also the NestedTextFormatter, which prints out your spec output in a way that follows how you nested your ExampleGroups and Examples.

    
    describe StuffController do
      describe "#create" do
        describe "when logged in" do
          describe "when passed valid data" do
            it "creates Stuff" do
            end
          end

      describe "when passed invalid data" do
        it "raises a RecordInvalid error and does not create Stuff" do
        end
      end
    end
    describe "when not logged in" do
      it "redirects to /login" do
        ...
      end
    end
    

    end end

    Prints out:

    
    StuffController
      #create
        when logged in
          when passed valid data
            creates Stuff
          when passed invalid data
            raises a RecordInvalid error and does not create Stuff
        when not logged in
          redirects to /login
    

    To give it a shot, pass in:

    
    script/spec spec/your_spec_file.rb --format nested
    
    or
    
    script/spec spec/your_spec_file.rb -fn