5 Things why I love RSpec

Mar 19
2007 09:31 (Development, RSpec, Ruby on Rails) · Русский (8,842 views)

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:

    assert('login', @user.login)

    and

    @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:

    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!

    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.

    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 '5 Things why I love RSpec'

Subscribe to comments with RSS or TrackBack to '5 Things why I love RSpec'.

1
said on 2007-03-21 at 12.00 pm

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

2
Alex
said on 2007-03-22 at 11.15 am

Yeah, rspec is a great tool!

3
said on 2007-04-06 at 4.35 pm

[...] Advantages of rspec [...]

Post a comment

You can use simple HTML-formatting tags (like <a>, <ul> and others). To format your code sample use <code lang="php">$a = "hello";</code> (allowed languages are ruby, php, yaml, html, csharp, javascript). Also you could use <code>$a = "hello";</code> and its syntax would not be highlighted. If you are not using <code> tag, replace < sign with &lt;.

Submit Comment

 
Copyright © 2005 - 2008, Dmytro Shteflyuk