From 390cc4cae470b15762bed78e3336bbbd1b1dcb2c Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Sat, 29 Jul 2017 13:47:39 +0200 Subject: [PATCH 01/24] Add first sign in feature specs This sets up capybara with the new headless chrome driver and adds the necessary requires in order to run single spec file. So far, there was no other way than doing `bundle exec rake` due to missing requires. --- Gemfile | 3 +++ Gemfile.lock | 18 ++++++++++++++++++ spec/features/sign_in_spec.rb | 29 +++++++++++++++++++++++++++++ spec/spec_helper.rb | 17 +++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 spec/features/sign_in_spec.rb diff --git a/Gemfile b/Gemfile index 5a1943a13..8b3f59444 100644 --- a/Gemfile +++ b/Gemfile @@ -60,6 +60,9 @@ group :test do gem 'shoulda', ">= 3.5" gem 'fabrication' gem 'faker' + gem 'capybara', '~> 2.4.4' + gem 'capybara-selenium', '~> 0.0.6' + gem 'chromedriver-helper', '~> 1.0' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 3032c85f9..9d58e4678 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -53,6 +53,8 @@ GEM sshkit (>= 1.6.1, != 1.7.0) arbre (1.1.1) activesupport (>= 3.0.0) + archive-zip (0.11.0) + io-like (~> 0.3.0) arel (6.0.4) ast (2.4.0) autoprefixer-rails (6.3.1) @@ -90,6 +92,14 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + capybara-selenium (0.0.6) + capybara + selenium-webdriver + childprocess (0.9.0) + ffi (~> 1.0, >= 1.0.11) + chromedriver-helper (1.2.0) + archive-zip (~> 0.10) + nokogiri (~> 1.8) chronic (0.10.2) coderay (1.1.2) coffee-rails (4.1.0) @@ -138,6 +148,7 @@ GEM i18n (~> 0.5) faraday (0.9.1) multipart-post (>= 1.2, < 3) + ffi (1.9.23) formtastic (3.1.5) actionpack (>= 3.2.13) formtastic_i18n (0.6.0) @@ -160,6 +171,7 @@ GEM has_scope (~> 0.6) railties (>= 4.2, <= 5.2) responders + io-like (0.3.0) jquery-rails (4.3.1) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) @@ -303,6 +315,7 @@ GEM ruby-progressbar (~> 1.7) unicode-display_width (~> 1.0, >= 1.0.1) ruby-progressbar (1.9.0) + rubyzip (1.2.1) sass (3.4.21) sass-rails (5.0.7) railties (>= 4.0.0, < 6) @@ -312,6 +325,9 @@ GEM tilt (>= 1.1, < 3) select2-rails (4.0.1) thor (~> 0.14) + selenium-webdriver (3.11.0) + childprocess (~> 0.5) + rubyzip (~> 1.2) shoulda (3.5.0) shoulda-context (~> 1.0, >= 1.0.1) shoulda-matchers (>= 1.4.1, < 3.0) @@ -376,6 +392,8 @@ DEPENDENCIES capistrano-rails (~> 1.1) capistrano-rbenv (~> 2.1) capybara (~> 2.4.4) + capybara-selenium (~> 0.0.6) + chromedriver-helper (~> 1.0) coffee-rails dalli database_cleaner (= 1.3.0) diff --git a/spec/features/sign_in_spec.rb b/spec/features/sign_in_spec.rb new file mode 100644 index 000000000..039a383f7 --- /dev/null +++ b/spec/features/sign_in_spec.rb @@ -0,0 +1,29 @@ +require 'spec_helper' + +describe 'sign in' do + let!(:user) do + Fabricate(:user, email: 'user@timeoverflow.org', password: 'papapa22') + end + + context 'with a valid password' do + it 'signs the user in' do + visit '/login' + fill_in 'user_email', with: user.email + fill_in 'user_password', with: 'papapa22' + click_button I18n.t('application.login_form.button') + + expect(page).to have_content(I18n.t('application.navbar.sign_out')) + end + end + + context 'with an invalid password' do + it 'shows an error' do + visit '/login' + fill_in 'user_email', with: user.email + fill_in 'user_password', with: 'wrong_password' + click_button I18n.t('application.login_form.button') + + expect(page).to have_content(I18n.t('devise.failure.invalid')) + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5f36a63ee..0340da70c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,9 +7,26 @@ require 'capybara/rails' require 'database_cleaner' require 'fabrication' +require 'selenium/webdriver' require 'faker' I18n.reload! +Capybara.register_driver :chrome do |app| + Capybara::Selenium::Driver.new(app, browser: :chrome) +end + +Capybara.register_driver :headless_chrome do |app| + capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( + chromeOptions: { args: %w(headless disable-gpu) } + ) + + Capybara::Selenium::Driver.new app, + browser: :chrome, + desired_capabilities: capabilities +end + +Capybara.javascript_driver = :headless_chrome + # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } From 03b8660aa2710f8c09e02a1bf3d190914c6e0cce Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Sat, 29 Jul 2017 14:20:26 +0200 Subject: [PATCH 02/24] Add sign out feature spec --- spec/features/sign_out_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 spec/features/sign_out_spec.rb diff --git a/spec/features/sign_out_spec.rb b/spec/features/sign_out_spec.rb new file mode 100644 index 000000000..77db37795 --- /dev/null +++ b/spec/features/sign_out_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe 'sign out' do + let!(:user) do + Fabricate(:user, email: 'user@timeoverflow.org', password: 'papapa22') + end + + it 'signs the user out' do + visit '/login' + fill_in 'user_email', with: user.email + fill_in 'user_password', with: 'papapa22' + click_button I18n.t('application.login_form.button') + + click_link I18n.t('application.navbar.sign_out') + + expect(current_path).to eq(root_path) + end +end From 450d0f80ac09c5b54924de5ec064cd9c48a304b4 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 1 Aug 2017 13:17:01 +0200 Subject: [PATCH 03/24] Install chrome in CI to run spec/features The headless chromedriver requires Chrome to be installed. See https://robots.thoughtbot.com/headless-feature-specs-with-chrome#on-ci for details. --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index cdb33fd2f..dcc62a00e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,3 +11,5 @@ before_script: - sleep 10 services: - elasticsearch +addons: + chrome: stable From 631f416df3112496606206176acdab813d5dd5ef Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 1 Aug 2017 14:57:15 +0200 Subject: [PATCH 04/24] Dry feature specs with #sign_in_with helper method --- spec/features/sign_in_spec.rb | 14 +++----------- spec/features/sign_out_spec.rb | 6 +----- spec/spec_helper.rb | 2 ++ spec/support/features/session_helpers.rb | 10 ++++++++++ 4 files changed, 16 insertions(+), 16 deletions(-) create mode 100644 spec/support/features/session_helpers.rb diff --git a/spec/features/sign_in_spec.rb b/spec/features/sign_in_spec.rb index 039a383f7..782ec4e5b 100644 --- a/spec/features/sign_in_spec.rb +++ b/spec/features/sign_in_spec.rb @@ -1,28 +1,20 @@ require 'spec_helper' describe 'sign in' do - let!(:user) do + let(:user) do Fabricate(:user, email: 'user@timeoverflow.org', password: 'papapa22') end context 'with a valid password' do it 'signs the user in' do - visit '/login' - fill_in 'user_email', with: user.email - fill_in 'user_password', with: 'papapa22' - click_button I18n.t('application.login_form.button') - + sign_in_with(user.email, 'papapa22') expect(page).to have_content(I18n.t('application.navbar.sign_out')) end end context 'with an invalid password' do it 'shows an error' do - visit '/login' - fill_in 'user_email', with: user.email - fill_in 'user_password', with: 'wrong_password' - click_button I18n.t('application.login_form.button') - + sign_in_with(user.email, 'wrong_password') expect(page).to have_content(I18n.t('devise.failure.invalid')) end end diff --git a/spec/features/sign_out_spec.rb b/spec/features/sign_out_spec.rb index 77db37795..a357144b3 100644 --- a/spec/features/sign_out_spec.rb +++ b/spec/features/sign_out_spec.rb @@ -6,11 +6,7 @@ end it 'signs the user out' do - visit '/login' - fill_in 'user_email', with: user.email - fill_in 'user_password', with: 'papapa22' - click_button I18n.t('application.login_form.button') - + sign_in_with(user.email, user.password) click_link I18n.t('application.navbar.sign_out') expect(current_path).to eq(root_path) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0340da70c..b84f4e8e5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -74,6 +74,8 @@ config.include Devise::Test::ControllerHelpers, :type => :controller config.include ControllerMacros, :type => :controller + config.include Features::SessionHelpers, type: :feature + # Database cleaner configuration config.before :suite do diff --git a/spec/support/features/session_helpers.rb b/spec/support/features/session_helpers.rb new file mode 100644 index 000000000..d9a686c19 --- /dev/null +++ b/spec/support/features/session_helpers.rb @@ -0,0 +1,10 @@ +module Features + module SessionHelpers + def sign_in_with(email, password) + visit '/login' + fill_in 'user_email', with: email + fill_in 'user_password', with: password + click_button I18n.t('application.login_form.button') + end + end +end From e60ceb8e684fb9068cb444c2bb5cd8ba6057f70b Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Tue, 1 Aug 2017 16:10:39 +0200 Subject: [PATCH 05/24] Remove unused transactional fixtures config We had `use_transactional_fixtures` enabled but no fixtures where defined in spec/fixtures. This prevented Capybara tests with javascript to work. This is explained in detail in https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example. --- spec/spec_helper.rb | 50 ++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b84f4e8e5..da2aa407d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -52,14 +52,6 @@ # config.mock_with :flexmock # config.mock_with :rr - # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures - config.fixture_path = "#{::Rails.root}/spec/fixtures" - - # If you're not using ActiveRecord, or you'd prefer not to run each of your - # examples within a transaction, remove the following line or assign false - # instead of true. - config.use_transactional_fixtures = true - # If true, the base class of anonymous controllers will be inferred # automatically. This will be the default behavior in future versions of # rspec-rails. @@ -76,25 +68,51 @@ config.include Features::SessionHelpers, type: :feature - # Database cleaner configuration - - config.before :suite do - DatabaseCleaner.strategy = :transaction - DatabaseCleaner.clean_with :truncation - - # Create terms and conditions + # Create terms and conditions + config.before do Document.create(label: "t&c") do |doc| doc.title = "Terms and Conditions" doc.content = "blah blah blah" end + end + # Database cleaner configuration + config.before(:suite) do + if config.use_transactional_fixtures? + raise(<<-MSG) + Delete line `config.use_transactional_fixtures = true` from rails_helper.rb + (or set it to false) to prevent uncommitted transactions being used in + JavaScript-dependent specs. + + During testing, the app-under-test that the browser driver connects to + uses a different database connection to the database connection used by + the spec. The app's database connection would not be able to access + uncommitted transaction data setup over the spec's database connection. + MSG + end + + DatabaseCleaner.strategy = :transaction + DatabaseCleaner.clean_with(:truncation) + end + + config.before(:each, type: :feature) do + # :rack_test driver's Rack app under test shares database connection + # with the specs, so continue to use transaction strategy for speed. + driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test + + if !driver_shares_db_connection_with_specs + # Driver is probably for an external browser with an app + # under test that does *not* share a database connection with the + # specs, so use truncation strategy. + DatabaseCleaner.strategy = :truncation + end end config.before(:each) do DatabaseCleaner.start end - config.after(:each) do + config.append_after(:each) do DatabaseCleaner.clean end From 1c2a3596b9e29291537ba8e4589b07989fdebbe9 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 11 Aug 2017 15:25:01 +0200 Subject: [PATCH 06/24] Add feature test for offer transfer between users --- spec/features/transfer_spec.rb | 52 ++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 spec/features/transfer_spec.rb diff --git a/spec/features/transfer_spec.rb b/spec/features/transfer_spec.rb new file mode 100644 index 000000000..179fc0748 --- /dev/null +++ b/spec/features/transfer_spec.rb @@ -0,0 +1,52 @@ +require 'spec_helper' + +describe 'time transfer', js: true do + let(:user) do + Fabricate(:user, email: 'user@timeoverflow.org', password: 'papapa22') + end + let(:other_user) do + Fabricate(:user, email: 'other_user@timeoverflow.org', password: 'papapa22') + end + let(:organization) { Fabricate(:organization) } + + before do + # Create terms and conditions + Document.create(label: "t&c") do |doc| + doc.title = "Terms and Conditions" + doc.content = "blah blah blah" + end + + user.add_to_organization(organization) + other_user.add_to_organization(organization) + end + + it 'transfers time from one account to another' do + offer = Fabricate(:offer, user: other_user, organization: organization) + sign_in_with(user.email, 'papapa22') + + within members_list do + click_link other_user.username + end + + click_link offer.title + click_link I18n.t('offers.show.give_time_for') + + within transfer_form do + fill_in 'transfer_hours', with: 2 + + # hack alert! there is no translation for this string. How we build the + # copy then? + click_button 'Crear Transferencia' + end + + expect(page).to have_css('.transactions tr', count: 1, text: '2:00') + end + + def members_list + find('.users tbody') + end + + def transfer_form + find('#new_transfer') + end +end From b8cc72ace005c54ff09d1a7cd705d8776418b210 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 11 Aug 2017 15:37:50 +0200 Subject: [PATCH 07/24] Abstract feature transfer spec --- spec/features/transfer_spec.rb | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/spec/features/transfer_spec.rb b/spec/features/transfer_spec.rb index 179fc0748..b7a75e875 100644 --- a/spec/features/transfer_spec.rb +++ b/spec/features/transfer_spec.rb @@ -22,24 +22,35 @@ it 'transfers time from one account to another' do offer = Fabricate(:offer, user: other_user, organization: organization) - sign_in_with(user.email, 'papapa22') - within members_list do - click_link other_user.username - end + sign_in_with(user.email, 'papapa22') + navigate_to_member + navigate_to_transfer_for(offer) + submit_transfer_form_with(hours: 2) - click_link offer.title - click_link I18n.t('offers.show.give_time_for') + expect(page).to have_css('.transactions tr', count: 1, text: '2:00') + end + def submit_transfer_form_with(hours: nil, minutes: nil) within transfer_form do - fill_in 'transfer_hours', with: 2 + fill_in 'transfer_hours', with: hours + fill_in 'transfer_minutes', with: minutes # hack alert! there is no translation for this string. How we build the # copy then? click_button 'Crear Transferencia' end + end - expect(page).to have_css('.transactions tr', count: 1, text: '2:00') + def navigate_to_transfer_for(offer) + click_link offer.title + click_link I18n.t('offers.show.give_time_for') + end + + def navigate_to_member + within members_list do + click_link other_user.username + end end def members_list From 2b9275c023560e8dcfeed85071c22decb16ef177 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Fri, 11 Aug 2017 18:48:03 +0200 Subject: [PATCH 08/24] Use the Travis' chrome addon in headless mode --- .travis.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.travis.yml b/.travis.yml index dcc62a00e..53b49ad64 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,3 +13,6 @@ services: - elasticsearch addons: chrome: stable + +before_install: + - google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost & From 3cfd9da5dfafa30d67af577d8ed117815920eb0e Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 16 Aug 2017 13:04:54 +0200 Subject: [PATCH 09/24] Fix style --- spec/spec_helper.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index da2aa407d..bf175f2d0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -20,9 +20,11 @@ chromeOptions: { args: %w(headless disable-gpu) } ) - Capybara::Selenium::Driver.new app, + Capybara::Selenium::Driver.new( + app, browser: :chrome, desired_capabilities: capabilities + ) end Capybara.javascript_driver = :headless_chrome @@ -63,9 +65,8 @@ # --seed 1234 config.order = "random" - config.include Devise::Test::ControllerHelpers, :type => :controller - config.include ControllerMacros, :type => :controller - + config.include Devise::TestHelpers, type: :controller + config.include ControllerMacros, type: :controller config.include Features::SessionHelpers, type: :feature # Create terms and conditions From 70454718f4fcf7b9b8e84c2222f090b87dc5bd08 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 16 Aug 2017 13:35:03 +0200 Subject: [PATCH 10/24] Make t&c creation fail fast I want to know if when the t&c could not be created because the tests assume they are always persisted in DB. --- spec/spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index bf175f2d0..385211584 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -71,7 +71,7 @@ # Create terms and conditions config.before do - Document.create(label: "t&c") do |doc| + Document.create!(label: "t&c") do |doc| doc.title = "Terms and Conditions" doc.content = "blah blah blah" end From ad099a572906c71d7a48355c3c8ae2099febdebd Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 16 Aug 2017 13:36:22 +0200 Subject: [PATCH 11/24] Fix database cleaner config --- spec/spec_helper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 385211584..6b36fd7ba 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -92,7 +92,6 @@ MSG end - DatabaseCleaner.strategy = :transaction DatabaseCleaner.clean_with(:truncation) end @@ -109,6 +108,10 @@ end end + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + config.before(:each) do DatabaseCleaner.start end From 771aac4dcc2ad276fb943459a8f224c1322cc135 Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 16 Aug 2017 17:12:14 +0200 Subject: [PATCH 12/24] Upgrade Capybara to 2.7 --- Gemfile | 3 +-- Gemfile.lock | 13 +++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index 8b3f59444..3b1693b43 100644 --- a/Gemfile +++ b/Gemfile @@ -49,7 +49,6 @@ end group :development, :test do gem "rspec-rails", '~> 3.7.2' - gem "capybara", '~> 2.4.4' gem "byebug" end @@ -60,7 +59,7 @@ group :test do gem 'shoulda', ">= 3.5" gem 'fabrication' gem 'faker' - gem 'capybara', '~> 2.4.4' + gem 'capybara', '~> 2.7' gem 'capybara-selenium', '~> 0.0.6' gem 'chromedriver-helper', '~> 1.0' end diff --git a/Gemfile.lock b/Gemfile.lock index 9d58e4678..f30c2b06b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -86,12 +86,13 @@ GEM capistrano-rbenv (2.1.3) capistrano (~> 3.1) sshkit (~> 1.3) - capybara (2.4.4) - mime-types (>= 1.16) + capybara (2.18.0) + addressable + mini_mime (>= 0.1.3) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) - xpath (~> 2.0) + xpath (>= 2.0, < 4.0) capybara-selenium (0.0.6) capybara selenium-webdriver @@ -375,8 +376,8 @@ GEM sprockets-rails (>= 2.0, < 4.0) whenever (0.9.4) chronic (>= 0.6.3) - xpath (2.0.0) - nokogiri (~> 1.3) + xpath (3.0.0) + nokogiri (~> 1.8) PLATFORMS ruby @@ -391,7 +392,7 @@ DEPENDENCIES capistrano (~> 3.1) capistrano-rails (~> 1.1) capistrano-rbenv (~> 2.1) - capybara (~> 2.4.4) + capybara (~> 2.7) capybara-selenium (~> 0.0.6) chromedriver-helper (~> 1.0) coffee-rails From 0a9f8f558cb5440d168555407e4c536f85f5955a Mon Sep 17 00:00:00 2001 From: Pau Perez Date: Wed, 16 Aug 2017 17:12:55 +0200 Subject: [PATCH 13/24] Load RSpec 2+ capybara support --- spec/spec_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6b36fd7ba..32348799e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,6 +5,7 @@ require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'capybara/rails' +require 'capybara/rspec' require 'database_cleaner' require 'fabrication' require 'selenium/webdriver' From 1512cd34a773274395b52cc70a3fcc39bad4cd96 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Sun, 4 Mar 2018 21:05:40 +0100 Subject: [PATCH 14/24] wtf --- spec/spec_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 32348799e..5b863e8ed 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -29,6 +29,7 @@ end Capybara.javascript_driver = :headless_chrome +Capybara.default_driver = :headless_chrome # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. From 0e89b13f8c5edcfe9e3206edcbba49e5b3a3b97c Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Thu, 15 Mar 2018 21:32:18 +0100 Subject: [PATCH 15/24] use capybara feature DSL so DatabaseCleaner uses truncation strategy --- spec/features/sign_in_spec.rb | 2 +- spec/features/sign_out_spec.rb | 2 +- spec/features/transfer_spec.rb | 2 +- spec/spec_helper.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/features/sign_in_spec.rb b/spec/features/sign_in_spec.rb index 782ec4e5b..9ba89fecf 100644 --- a/spec/features/sign_in_spec.rb +++ b/spec/features/sign_in_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'sign in' do +feature 'sign in' do let(:user) do Fabricate(:user, email: 'user@timeoverflow.org', password: 'papapa22') end diff --git a/spec/features/sign_out_spec.rb b/spec/features/sign_out_spec.rb index a357144b3..68d99b8fa 100644 --- a/spec/features/sign_out_spec.rb +++ b/spec/features/sign_out_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'sign out' do +feature 'sign out' do let!(:user) do Fabricate(:user, email: 'user@timeoverflow.org', password: 'papapa22') end diff --git a/spec/features/transfer_spec.rb b/spec/features/transfer_spec.rb index b7a75e875..de4d0b3fa 100644 --- a/spec/features/transfer_spec.rb +++ b/spec/features/transfer_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe 'time transfer', js: true do +feature 'time transfer' do let(:user) do Fabricate(:user, email: 'user@timeoverflow.org', password: 'papapa22') end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5b863e8ed..e8c07e42a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -102,7 +102,7 @@ # with the specs, so continue to use transaction strategy for speed. driver_shares_db_connection_with_specs = Capybara.current_driver == :rack_test - if !driver_shares_db_connection_with_specs + unless driver_shares_db_connection_with_specs # Driver is probably for an external browser with an app # under test that does *not* share a database connection with the # specs, so use truncation strategy. From f129448419d479eada0869c6bff9560c46ca9a5a Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Thu, 15 Mar 2018 21:51:06 +0100 Subject: [PATCH 16/24] fix sign_in spec ( read cookies to assert sign_in status ) --- spec/features/sign_in_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/features/sign_in_spec.rb b/spec/features/sign_in_spec.rb index 9ba89fecf..e5b9479c5 100644 --- a/spec/features/sign_in_spec.rb +++ b/spec/features/sign_in_spec.rb @@ -7,8 +7,11 @@ context 'with a valid password' do it 'signs the user in' do + expect(Capybara.current_session.driver.browser.manage.cookie_named('_timeoverflow_session')).to be_falsy + sign_in_with(user.email, 'papapa22') - expect(page).to have_content(I18n.t('application.navbar.sign_out')) + + expect(Capybara.current_session.driver.browser.manage.cookie_named('_timeoverflow_session')).to be_truthy end end From 318ee22d94487a1953142dc49a94dc51b00e26d0 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Sun, 18 Mar 2018 20:23:39 +0100 Subject: [PATCH 17/24] bump database_cleaner --- Gemfile | 4 +--- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 3b1693b43..fc93e86d9 100644 --- a/Gemfile +++ b/Gemfile @@ -53,9 +53,7 @@ group :development, :test do end group :test do - # Do not upgrade until - # https://github.com/DatabaseCleaner/database_cleaner/issues/317 is fixed - gem "database_cleaner", '1.3.0' + gem "database_cleaner", '1.6.2' gem 'shoulda', ">= 3.5" gem 'fabrication' gem 'faker' diff --git a/Gemfile.lock b/Gemfile.lock index f30c2b06b..1bd435d94 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -114,7 +114,7 @@ GEM concurrent-ruby (1.0.5) crass (1.0.3) dalli (2.7.2) - database_cleaner (1.3.0) + database_cleaner (1.6.2) debug_inspector (0.0.3) devise (4.4.1) bcrypt (~> 3.0) @@ -397,7 +397,7 @@ DEPENDENCIES chromedriver-helper (~> 1.0) coffee-rails dalli - database_cleaner (= 1.3.0) + database_cleaner (= 1.6.2) devise (~> 4.4.1) dotenv-rails (= 1.0.2) elasticsearch-model From c4e3db4e1ae5e6a5d3cc12bc543385dfea8929a6 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Sun, 18 Mar 2018 20:23:49 +0100 Subject: [PATCH 18/24] fix database cleaner conf --- spec/spec_helper.rb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e8c07e42a..0afe57a2b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -79,24 +79,28 @@ end end - # Database cleaner configuration + config.use_transactional_fixtures = false + config.before(:suite) do if config.use_transactional_fixtures? raise(<<-MSG) - Delete line `config.use_transactional_fixtures = true` from rails_helper.rb - (or set it to false) to prevent uncommitted transactions being used in - JavaScript-dependent specs. - - During testing, the app-under-test that the browser driver connects to - uses a different database connection to the database connection used by - the spec. The app's database connection would not be able to access - uncommitted transaction data setup over the spec's database connection. + Delete line `config.use_transactional_fixtures = true` from rails_helper.rb + (or set it to false) to prevent uncommitted transactions being used in + JavaScript-dependent specs. + + During testing, the app-under-test that the browser driver connects to + uses a different database connection to the database connection used by + the spec. The app's database connection would not be able to access + uncommitted transaction data setup over the spec's database connection. MSG end - DatabaseCleaner.clean_with(:truncation) end + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + config.before(:each, type: :feature) do # :rack_test driver's Rack app under test shares database connection # with the specs, so continue to use transaction strategy for speed. @@ -110,10 +114,6 @@ end end - config.before(:each) do - DatabaseCleaner.strategy = :transaction - end - config.before(:each) do DatabaseCleaner.start end From 9f91e0201716daea7f376f5f7a69149e34de1709 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Sun, 18 Mar 2018 20:24:02 +0100 Subject: [PATCH 19/24] better assertions in sign_in spec --- spec/features/sign_in_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/features/sign_in_spec.rb b/spec/features/sign_in_spec.rb index e5b9479c5..a7abc778d 100644 --- a/spec/features/sign_in_spec.rb +++ b/spec/features/sign_in_spec.rb @@ -9,9 +9,10 @@ it 'signs the user in' do expect(Capybara.current_session.driver.browser.manage.cookie_named('_timeoverflow_session')).to be_falsy - sign_in_with(user.email, 'papapa22') + sign_in_with(user.email, user.password) expect(Capybara.current_session.driver.browser.manage.cookie_named('_timeoverflow_session')).to be_truthy + expect(page).to have_no_content(I18n.t('devise.failure.invalid')) end end From 96a75f281490b2675b5d42c9224233736222e304 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Sun, 18 Mar 2018 20:42:10 +0100 Subject: [PATCH 20/24] fix transfer spec --- spec/features/transfer_spec.rb | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/spec/features/transfer_spec.rb b/spec/features/transfer_spec.rb index de4d0b3fa..3573a3dfc 100644 --- a/spec/features/transfer_spec.rb +++ b/spec/features/transfer_spec.rb @@ -2,28 +2,30 @@ feature 'time transfer' do let(:user) do - Fabricate(:user, email: 'user@timeoverflow.org', password: 'papapa22') - end - let(:other_user) do - Fabricate(:user, email: 'other_user@timeoverflow.org', password: 'papapa22') + user = Fabricate( + :user, + email: 'user@timeoverflow.org', + password: 'papapa22', + terms_accepted_at: 1.day.from_now + ) + + user.add_to_organization(organization) + + user end - let(:organization) { Fabricate(:organization) } - before do - # Create terms and conditions - Document.create(label: "t&c") do |doc| - doc.title = "Terms and Conditions" - doc.content = "blah blah blah" - end + let(:other_user) do + other_user = Fabricate(:user, email: 'other_user@timeoverflow.org', password: 'papapa22') - user.add_to_organization(organization) other_user.add_to_organization(organization) + other_user end + let(:organization) { Fabricate(:organization) } + it 'transfers time from one account to another' do offer = Fabricate(:offer, user: other_user, organization: organization) - - sign_in_with(user.email, 'papapa22') + sign_in_with(user.email, user.password) navigate_to_member navigate_to_transfer_for(offer) submit_transfer_form_with(hours: 2) @@ -36,8 +38,6 @@ def submit_transfer_form_with(hours: nil, minutes: nil) fill_in 'transfer_hours', with: hours fill_in 'transfer_minutes', with: minutes - # hack alert! there is no translation for this string. How we build the - # copy then? click_button 'Crear Transferencia' end end From 68e96da9b79b54c0fe3b38bd74d04431a00243f5 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Tue, 20 Mar 2018 19:51:52 +0100 Subject: [PATCH 21/24] configuring chrome in travis.yml --- .travis.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 53b49ad64..3d8884632 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: ruby -sudo: false +sudo: required cache: bundler bundler_args: '--without production development' rvm: @@ -13,6 +13,3 @@ services: - elasticsearch addons: chrome: stable - -before_install: - - google-chrome-stable --headless --disable-gpu --remote-debugging-port=9222 http://localhost & From 90ac1d456bfa6e623a70d7392d273f9f9dd563d4 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Sun, 25 Mar 2018 15:27:00 +0200 Subject: [PATCH 22/24] fix sign out spec --- spec/features/sign_out_spec.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/features/sign_out_spec.rb b/spec/features/sign_out_spec.rb index 68d99b8fa..7199b6fa0 100644 --- a/spec/features/sign_out_spec.rb +++ b/spec/features/sign_out_spec.rb @@ -2,11 +2,17 @@ feature 'sign out' do let!(:user) do - Fabricate(:user, email: 'user@timeoverflow.org', password: 'papapa22') + Fabricate( + :user, + email: 'user@timeoverflow.org', + password: 'papapa22', + terms_accepted_at: 1.day.from_now + ) end it 'signs the user out' do sign_in_with(user.email, user.password) + click_link user.email click_link I18n.t('application.navbar.sign_out') expect(current_path).to eq(root_path) From 94c50fbec07ae4fcecfd7d31914456d42d5b1dc1 Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Sun, 25 Mar 2018 16:12:06 +0200 Subject: [PATCH 23/24] hack alert: run feature specs at the end, truncating mess ups other tests --- spec/spec_helper.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0afe57a2b..43434d797 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -65,7 +65,23 @@ # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 - config.order = "random" + + puts "Randomized with seed #{config.seed}." + + config.register_ordering(:global) do |items| + items_by_type = items.group_by { |item| item.metadata[:type] === :feature ? :feature : :rest } + + feature_specs = items_by_type[:feature] || [] + rest_of_specs = items_by_type[:rest] || [] + + + random = Random.new(config.seed) + + [ + *rest_of_specs.shuffle(random: random), + *feature_specs.shuffle(random: random) + ] + end config.include Devise::TestHelpers, type: :controller config.include ControllerMacros, type: :controller From 1ace872bd46e7eee96d02b4e9e3c74429fecac4d Mon Sep 17 00:00:00 2001 From: Jorge Morante Date: Sun, 1 Apr 2018 13:33:23 +0200 Subject: [PATCH 24/24] create offer spec WIP --- spec/features/create_offer_spec.rb | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 spec/features/create_offer_spec.rb diff --git a/spec/features/create_offer_spec.rb b/spec/features/create_offer_spec.rb new file mode 100644 index 000000000..1a7c2de07 --- /dev/null +++ b/spec/features/create_offer_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +feature 'create offer' do + let(:user) do + user = Fabricate( + :user, + email: 'user@timeoverflow.org', + password: 'papapa22', + terms_accepted_at: 1.day.from_now + ) + + user.add_to_organization(organization) + + user + end + + let(:organization) { Fabricate(:organization) } + + let!(:category) do + Category.create(name: 'le category') + end + + context 'create an offer' do + it 'can be created' do + sign_in_with(user.email, user.password) + click_on I18n.t('activerecord.models.offer.other') + click_on I18n.t('helpers.submit.create', model: I18n.t('activerecord.models.offer.one')) + fill_in 'offer_title', with: 'Le title' + fill_in 'offer_description', with: 'Lorem ipsum in the night' + select category.name, from: 'offer_category_id' + + # TODO there are two i18n keys for getting "Crear oferta" copy ( one returns 'Crear oferta' and the other 'Crear Oferta' ) + click_on I18n.t('offers.new.submit', model: I18n.t('activerecord.models.offer.one')) + + page.save_screenshot('create-offer-yolo.png') + end + end +end +