RSpec-1.1.4

May 26th, 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.

hash_including

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’t care about that extra data.

Enter hash_including().

This is a mock argument matcher that let’s you expect a hash including certain key/value pairs regardless of anything else that shows up in the hash. So instead of:

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

Thanks to Rick DeNatale who submitted this feature request and the patch to implement it.

The heckler returns

RSpec wasn’t correctly supporting heckle for a while but the spec-heckler is back in action. For those unfamiliar, you can read about heckle at zenspider’s blog.

Here’s how you heckle your Animal model in your PetStore app:

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

Thanks to Antti Tarvainen for resurrecting this one.

stub_model

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

Instead of creating a mock object like mock_model() does, stub_model() creates an instance of a real model class, but cuts off it’s connection to the database, raising an error any time it tries to connect to the database.

This is inspired by projects like unit_record and NullDB, but let’s you do things at a more granular level – allowing you to hit the db in some cases (where you think you really need it) and not in others.

Of course, you may prefer to the sort of “protection” 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.

All this and more

These are just a few of the issues addressed in 1.1.4. For more information, check out the changelog and lighthouse.

6 Responses to “RSpec-1.1.4”

  1. thilo
    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
    Thomas Maurer Says:

    its on github.com now.

  3. Luke Redpath
    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.

    Mocha Parameter Matchers RDoc

  4. Foo Bar
    Foo Bar Says:

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

  5. Collin VanDyck
    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
    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
    

Leave a Reply (Textile Enabled)