Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Speed up rspec tests - part 1

109 views

Published on

Kamil, our CTO has shared with us his knowledge of RSpec! The title of the presentation is "Speed up rspec tests - part 1". Stay tuned for the next part!

Published in: Software
  • Be the first to comment

  • Be the first to like this

Speed up rspec tests - part 1

  1. 1. SPEED UP RSPEC TESTS - PART1 Kamil Baćkowski
  2. 2. EXAMPLE FEATURE SPEC describe 'Lists', type: :feature, js: true do before do # time consuming setup which takes about 6s end # each it takes about 1 second it 'should be able to browse lists' do end it 'should be able to edit a list' do end it 'should be able to add companies to list' do end it 'should be able to remove companies from list' do end it 'should be able to delete a list' do end end
  3. 3. HOW LONG IT TAKES ? rspec spec/features/lists_spec.rb ..... Finished in 35.34 seconds (files took 9.99 seconds to load) 5 examples, 0 failures
  4. 4. HOW TO SPEED UP ? We can combine all tests cases into one so that before hook will execute only once. describe 'Lists', type: :feature, js: true do before do # time consuming setup which takes about 6s end it "should be able to manage lists" do #should be able to edit a list ... #should be able to add companies to list ... #should be able to remove companies from list ... #should be able to delete a list ... end end
  5. 5. WE CAN DO IT BETTER! Use aggregate_failures from rspec require 'spec_helper' describe 'Lists', type: :feature, js: true do before do # time consuming setup which takes about 6s end it 'should be able to manage lists' do aggregate_failures 'should be able to browse lists' do end aggregate_failures 'should be able to edit a list' do end aggregate_failures 'should be able to add companies to list' do end aggregate_failures 'should be able to remove companies from list' do end aggregate_failures 'should be able to delete a list' do end end end
  6. 6. HOW LONG IT TAKES ? rspec spec/features/lists_spec.rb . Finished in 11.23 seconds (files took 9.99 seconds to load) 1 example, 0 failures
  7. 7. PROS & CONS Tests runs 3x times faster Tests becomes dependant on each other
  8. 8. WHAT HAPPENS WHEN SOME TEST FAILS ? By default all next aggregate_failures within block will not execute We can change this by adding aggregate_failures metadata and then all aggregate_failures will be executed and rspec will report all errors.
  9. 9. WHAT ABOUT CONTROLLERS ? describe CompanyOfficesController, type: :controller do describe '#create' do it 'validates params' do sign_in user post :create, name: '' expect(response).to render_template('new') expect(flash[:error]).not_to be_nil end it 'creates record when params are valid' do sign_in user expect do post :create, name: 'My office' end.to change(CompanyOffice, :count).by 1 end it 'creates record when params are valid and redirect to index' do sign_in user post :create, name: 'My office' expect(response).to redirect_to(company_offices_path) end end end
  10. 10. USING AGGREGATE_FAILURES describe CompanyOfficesController, type: :controller do describe '#create' do it 'creates office' do sign_in user aggregate_failures 'validates params' do post :create, name: '' expect(response).to render_template('new') expect(flash[:error]).not_to be_nil end aggregate_failures 'creates record when params are valid and redirect' do expect do post :create, name: 'My office' end.to change(CompanyOffice, :count).by 1 expect(response).to redirect_to(company_offices_path) end end end end
  11. 11. SUMMARY I encourage to use this for all features specs and some controller specs which uses the same setup. Questions ?

×