RSpec is an awesome testing framework. On top of being the first Ruby BDD framework the core team is doing a great job in enhancing our testing experience and therefore the quality of our code.

This time, I don’t want to introduce to the latest changes but instead showing you what Josh Knowles, Bryan Helmkamp and myself came up with.

RSpec on Rails matchers plugin + TextMate Bundle

Matchers are some sort of helpers that will help you cleaning up your tests. We simply came up with a collection of matchers that we think will make your like easier.

We divided the matchers in 3 categories:

Associations

Verify that the association has been defined. (doesn’t verify that the association works!)

Usage examples:

1
2
3
4
5
6
7
8

    @post.should have_many(:comments)
  
    @comment.should belong_to(:post)
  
    @user.should have_one(:social_security_number)
  
    @project.should have_and_belong_to_many(:categories)

Validations

Verify that a validation has been defined. (doesn’t test the validation itself)

1
2
3
4
5
6
7
8
9
10

    object.should validate_presence_of(:attribute)

    object.should validate_confirmation_of(:attribute)

    object.should validate_uniqueness_of(:attribute)

    object.should validate_length_of(:attribute, :between => 5..10)
    
    object.should validate_length_of(:attribute, :is => 5)

Views

My personal favorite matchers, you can now do stuff like:

1
2
3
4
5
6
7
8
9
10
11
12

    it "should render new form" do
        render "/users/new.html.erb"

        response.should have_form_posting_to(users_path) do
          with_text_field_for(:user_name)
          with_text_area_for(:user_address)
          with_text_field_for(:user_login)
          with_text_field_for(:user_email)
          with_submit_button
        end
    end

Check the readme for more information and details on the added matchers. I personally recommend you try the TextMate Bundle on top of being a perfect tool for lazy devs, it also lists all the available matchers and is an excellent way of learning.

We just released our first release yesterday, this is not a final version and we will keep on improving the code. If you have suggestions and patches feel free to open a ticket there.