From 76e6b8f5ec4cce0cc0d3633a329fc92c83010085 Mon Sep 17 00:00:00 2001 From: nilsding Date: Mon, 17 Nov 2014 23:25:28 +0100 Subject: [PATCH 1/3] tests. --- Gemfile | 2 ++ Gemfile.lock | 20 ++++++++++++++++++++ config/environments/test.rb | 2 +- spec/features/user_feature_spec.rb | 23 +++++++++++++++++++++++ spec/models/user_spec.rb | 1 - spec/requests/user_request_spec.rb | 10 ++++------ spec/spec_helper.rb | 6 ++++++ spec/support/wait_for_ajax.rb | 15 +++++++++++++++ 8 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 spec/features/user_feature_spec.rb create mode 100644 spec/support/wait_for_ajax.rb diff --git a/Gemfile b/Gemfile index 3fe0551e..188ecbc4 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,8 @@ group :development, :test do gem 'shoulda-matchers' gem 'factory_girl_rails' gem 'faker' + gem 'capybara' + gem 'poltergeist' gem 'simplecov' end diff --git a/Gemfile.lock b/Gemfile.lock index b01b7f3f..1f23ed11 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -35,6 +35,13 @@ GEM sass (~> 3.2) bootstrap_form (2.2.0) builder (3.2.2) + capybara (2.4.3) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) + cliver (0.3.2) coffee-rails (4.1.0) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -85,12 +92,20 @@ GEM mail (2.6.1) mime-types (>= 1.16, < 3) mime-types (2.4.3) + mini_portile (0.6.0) minitest (5.4.2) multi_json (1.10.1) mysql2 (0.3.16) + nokogiri (1.6.3.1) + mini_portile (= 0.6.0) nprogress-rails (0.1.6.3) orm_adapter (0.5.0) pg (0.17.1) + poltergeist (1.5.1) + capybara (~> 2.1) + cliver (~> 0.3.1) + multi_json (~> 1.0) + websocket-driver (>= 0.2.0) rack (1.5.2) rack-test (0.6.2) rack (>= 1.0) @@ -177,7 +192,10 @@ GEM raindrops (~> 0.7) warden (1.2.3) rack (>= 1.0) + websocket-driver (0.4.0) will_paginate (3.0.7) + xpath (2.0.0) + nokogiri (~> 1.3) PLATFORMS ruby @@ -187,6 +205,7 @@ DEPENDENCIES bootstrap-material-design bootstrap-sass (~> 3.2.0.1) bootstrap_form + capybara coffee-rails (~> 4.1.0) devise factory_girl_rails @@ -201,6 +220,7 @@ DEPENDENCIES mysql2 nprogress-rails pg + poltergeist rails (= 4.1.7) rspec-rails (~> 3.0.0) sass-rails (~> 4.0.3) diff --git a/config/environments/test.rb b/config/environments/test.rb index 053f5b66..411eb23c 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -21,7 +21,7 @@ Rails.application.configure do config.action_controller.perform_caching = false # Raise exceptions instead of rendering exception templates. - config.action_dispatch.show_exceptions = false + config.action_dispatch.show_exceptions = true # was: false # Disable request forgery protection in test environment. config.action_controller.allow_forgery_protection = false diff --git a/spec/features/user_feature_spec.rb b/spec/features/user_feature_spec.rb new file mode 100644 index 00000000..5a9c7c0a --- /dev/null +++ b/spec/features/user_feature_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +feature "User", :type => :feature do + before do + @user1 = create(:user) + @user2 = create(:user) + end + + scenario "gets asked a question", js: true do + visit "/@#{@user1.screen_name}" + + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}.png"), full: true + + fill_in "qb-question", with: Faker::Lorem.sentence + click_button "Ask" + wait_for_ajax + reload_page + + page.driver.render Rails.root.join('tmp'), full: full + + expect(page).to have_text("Question asked successfully.") + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index dcc9df96..2ce876dd 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,5 +1,4 @@ require 'rails_helper' RSpec.describe User, :type => :model do - # TODO: specs for user model end diff --git a/spec/requests/user_request_spec.rb b/spec/requests/user_request_spec.rb index 21152b25..d7613d47 100644 --- a/spec/requests/user_request_spec.rb +++ b/spec/requests/user_request_spec.rb @@ -1,14 +1,12 @@ require 'rails_helper' -RSpec.describe UserController, :type => :request do +RSpec.describe "user page", :type => :request do before do - # we need at least 2 users to test meaningfully - @user1 = create(:user) - @user2 = create(:user) + @user = create(:user) end it 'shows the user page' do - get "/@#{@user1.screen_name}" - assert_select "h3.text-muted", :text => @user1.screen_name + get "/@#{@user.screen_name}" + assert_select "h3.text-muted", :text => @user.screen_name end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b81d6eb5..919f3d9e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,12 @@ require 'simplecov' SimpleCov.start +# require 'capybara/rails' +# require 'capybara/rspec' +require 'capybara/poltergeist' +Capybara.default_driver = :poltergeist +Capybara.javascript_driver = :poltergeist + # This file was generated by the `rails generate rspec:install` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause this diff --git a/spec/support/wait_for_ajax.rb b/spec/support/wait_for_ajax.rb new file mode 100644 index 00000000..9d772404 --- /dev/null +++ b/spec/support/wait_for_ajax.rb @@ -0,0 +1,15 @@ +module WaitForAjax + def wait_for_ajax + Timeout.timeout(Capybara.default_wait_time) do + loop until finished_all_ajax_requests? + end + end + + def finished_all_ajax_requests? + page.evaluate_script('jQuery.active').zero? + end +end + +RSpec.configure do |config| + config.include WaitForAjax, type: :feature +end From c4d48190b636302ec1b5f1dc9f6ef1f580734c4c Mon Sep 17 00:00:00 2001 From: nilsding Date: Tue, 18 Nov 2014 19:58:14 +0100 Subject: [PATCH 2/3] added database_cleaner --- Gemfile | 4 ++-- Gemfile.lock | 5 ++--- spec/rails_helper.rb | 2 +- spec/support/database_cleaner.rb | 21 +++++++++++++++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 spec/support/database_cleaner.rb diff --git a/Gemfile b/Gemfile index 188ecbc4..705d9f6e 100644 --- a/Gemfile +++ b/Gemfile @@ -28,7 +28,6 @@ gem 'nprogress-rails' gem 'font-awesome-rails', '~> 4.2.0.0' group :development do - gem 'thin' gem 'spring' end @@ -37,12 +36,13 @@ group :production do end group :development, :test do + gem 'thin' gem 'rspec-rails', '~> 3.0.0' - gem 'shoulda-matchers' gem 'factory_girl_rails' gem 'faker' gem 'capybara' gem 'poltergeist' gem 'simplecov' + gem 'database_cleaner' end diff --git a/Gemfile.lock b/Gemfile.lock index 1f23ed11..05abc76e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -50,6 +50,7 @@ GEM execjs coffee-script-source (1.8.0) daemons (1.1.9) + database_cleaner (1.3.0) devise (3.4.1) bcrypt (~> 3.0) orm_adapter (~> 0.1) @@ -155,8 +156,6 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) - shoulda-matchers (2.7.0) - activesupport (>= 3.0.0) simplecov (0.9.1) docile (~> 1.1.0) multi_json (~> 1.0) @@ -207,6 +206,7 @@ DEPENDENCIES bootstrap_form capybara coffee-rails (~> 4.1.0) + database_cleaner devise factory_girl_rails faker @@ -225,7 +225,6 @@ DEPENDENCIES rspec-rails (~> 3.0.0) sass-rails (~> 4.0.3) sdoc (~> 0.4.1) - shoulda-matchers simplecov spring thin diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index ecb15e3d..5bbadc73 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -24,7 +24,7 @@ RSpec.configure do |config| # 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 + config.use_transactional_fixtures = false # RSpec Rails can automatically mix in different behaviours to your tests # based on their file location, for example enabling you to call `get` and diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 00000000..d88b24be --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,21 @@ +RSpec.configure do |config| + config.before(:suite) do + DatabaseCleaner.clean_with :truncation + end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each, js: true) do + DatabaseCleaner.strategy = :truncation + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end +end From c801fa34307e3ecff7795f8afe4038f7ddddaa2a Mon Sep 17 00:00:00 2001 From: nilsding Date: Tue, 18 Nov 2014 20:58:55 +0100 Subject: [PATCH 3/3] fixed tests --- spec/features/user_feature_spec.rb | 5 ++--- spec/spec_helper.rb | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/features/user_feature_spec.rb b/spec/features/user_feature_spec.rb index 5a9c7c0a..4cf00539 100644 --- a/spec/features/user_feature_spec.rb +++ b/spec/features/user_feature_spec.rb @@ -9,14 +9,13 @@ feature "User", :type => :feature do scenario "gets asked a question", js: true do visit "/@#{@user1.screen_name}" - page.driver.render Rails.root.join("tmp/#{Time.now.to_i}.png"), full: true + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_1.png"), full: true fill_in "qb-question", with: Faker::Lorem.sentence click_button "Ask" wait_for_ajax - reload_page - page.driver.render Rails.root.join('tmp'), full: full + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_2.png"), full: true expect(page).to have_text("Question asked successfully.") end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 919f3d9e..a5f6554f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,10 +1,10 @@ require 'simplecov' SimpleCov.start -# require 'capybara/rails' -# require 'capybara/rspec' require 'capybara/poltergeist' -Capybara.default_driver = :poltergeist +Capybara.register_driver :poltergeist do |app| + Capybara::Poltergeist::Driver.new(app, js_errors: false) +end Capybara.javascript_driver = :poltergeist # This file was generated by the `rails generate rspec:install` command. Conventionally, all