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)
firstname.lastname@example.org == '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
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).friended.first.should == users(:bob)
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
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
15context 'Given an signup action of UserController' do
@user = mock('user')
add_stubs(@user, :password= => nil, :password_confirmation= => nil, :new_password= => nil)
specify 'should render edit template with empty User object' do
This plugin shine a light over hours of frustrating test sessions :) Thank you !
Yeah, rspec is a great tool!
[…] Advantages of rspec […]