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.
-
I stopped getting confused with order of expected and real values. Compare:
1assert('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.
-
I could understand what objects should do at first look on the test:
1
2
3
4
5
6
7context '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 -
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
9The 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 -
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.
-
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
15context '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
This plugin shine a light over hours of frustrating test sessions :) Thank you !
Yeah, rspec is a great tool!
[…] Advantages of rspec […]