5 Things why I love RSpec

Posted by Dmytro Shteflyuk on under Ruby & Rails

RSpec provides a framework for writing what can be called executable specifications of program behavior. In this short post I want to explain why I use this framework in place of classic TestUnit library.

  1. I stopped getting confused with order of expected and real values. Compare:

    1
    assert('login', @user.login)

    and

    1
    @user.login.should == 'login'

    BTW, I made a mistake in the assert_equal parameters order when was writing this article.

  2. I could understand what objects should do at first look on the test:

    1
    2
    3
    4
    5
    6
    7
    context 'The User model with fixtures loaded' do
      specify 'should be friended by the user if this user added him to friends' do
        users(:bob).friends << users(:chris)
        users(:chris).should have(1).friended
        users(:chris).friended.first.should == users(:bob)
      end
    end
  3. Test results are presented in easy to understand and clear form, just simple sentences, which are making asserts about entities in the system. In most cases there is even no necessity to look at the tests to understand which functionality has one or another entity!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    The newly created User
    - should be valid with all neccessary attributes specified
    - should be invalid without login
    - should be invalid when login length is less than 2
    - should be invalid without email
    - should be invalid with wrong email
    - should be invalid with someone else's login
    - should be invalid with someone else's email
    - should not require password and password_confirmation
  4. I started writing tests before actual coding finally! In fact, every developer think over what one or another method should do, and frequently these thoughts looks like “when method received string ‘somestring’ it should return ‘anotherstring'”. It’s easy to describe these thoughts using BDD-tests.

  5. Integrated flexible mock-objects system allows testing functionality just once. For example, when you test controllers, you should not use database and models – it is sufficient to check, if necessary model methods would be called in right order with right parameters.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    context 'Given an signup action of UserController' do
      controller_name 'user'
     
      setup do
        @user = mock('user')
        add_stubs(@user, :password= => nil, :password_confirmation= => nil, :new_password= => nil)
      end
     
      specify 'should render edit template with empty User object' do
        User.should_receive(:new).with(:no_args).and_return(@user)
        get :signup
        response.should render_template(:edit)
        assigns[:user].should be(@user)
      end
    end

3 Responses to this entry

Subscribe to comments with RSS

said on March 21st, 2007 at 12:00 · Permalink

This plugin shine a light over hours of frustrating test sessions :) Thank you !

Alex
said on March 22nd, 2007 at 11:15 · Permalink

Yeah, rspec is a great tool!

Comments are closed

Comments for this entry are closed for a while. If you have anything to say – use a contact form. Thank you for your patience.