diff --git a/Gemfile b/Gemfile index 3fe0551e..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,10 +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 b01b7f3f..05abc76e 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) @@ -43,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) @@ -85,12 +93,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) @@ -140,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) @@ -177,7 +191,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,7 +204,9 @@ DEPENDENCIES bootstrap-material-design bootstrap-sass (~> 3.2.0.1) bootstrap_form + capybara coffee-rails (~> 4.1.0) + database_cleaner devise factory_girl_rails faker @@ -201,11 +220,11 @@ DEPENDENCIES mysql2 nprogress-rails pg + poltergeist rails (= 4.1.7) rspec-rails (~> 3.0.0) sass-rails (~> 4.0.3) sdoc (~> 0.4.1) - shoulda-matchers simplecov spring thin 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..4cf00539 --- /dev/null +++ b/spec/features/user_feature_spec.rb @@ -0,0 +1,22 @@ +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}_1.png"), full: true + + fill_in "qb-question", with: Faker::Lorem.sentence + click_button "Ask" + wait_for_ajax + + page.driver.render Rails.root.join("tmp/#{Time.now.to_i}_2.png"), full: true + + 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/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/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..a5f6554f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,12 @@ require 'simplecov' SimpleCov.start +require 'capybara/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 # 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/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 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