My top 7 RSpec best practices

Posted by Dmytro Shteflyuk on under Ruby & Rails (25,540 views)

BDD kinkier than TDD

I use RSpec in all my projects. It’s really hard to overemphasize how helpful it is and how much easier becomes your life if you have good specs coverage. But its outstanding flexibility enables many ways to make your specs awful: horribly slow, over-bloated, even non-readable sometimes. I do not want to teach you BDD and RSpec here, but instead I will give you some ideas how to improve your specs quality and increase efficiency of your BDD workflow.

Read the rest of entry »

Удобные хелперы для мок-объектов RSpec

Posted by Dmytro Shteflyuk on under Ruby & Rails · English (25,697 views)

В RSpec есть такая замечательная штука, как мок-объекты (mocks). В двух словах: мок-объект имитирует поведение объекта, который используется тестируемыми методами. Сразу простой пример:

1
2
3
4
5
6
7
8
9
10
describe UserHelper
  it 'should generate correct link to user profile in user_link' do
    @user = mock('User')
    @user.stub!(:id, 10)
    @user.stub!(:new_record?, false)
    @user.stub!(:preferred_name, 'Dmytro S.')
    @user.stub!(:full_name, 'Dmytro Shteflyuk')
    user_link(@user).should == link_to('Dmytro S.', user_url(:id => 10), :title => 'Dmytro Shteflyuk')
  end
end

Read the rest of entry »