From fba174e9bc4e4ef5c0c4d6a4282f37e5265b87e2 Mon Sep 17 00:00:00 2001
From: Robert Speicher <rspeicher@gmail.com>
Date: Thu, 23 Aug 2012 05:19:40 -0400
Subject: [PATCH] Cleanup spec/support folder and spec/spec_helper

Changes:
* Move spec/monkeypatch to spec/support
* Remove unused support/shared_examples
* Move support/api to support/api_helpers to match module name
* Move support/login to support/login_helpers to match module name
* Move API specs to requests/api (convention over configuration)
* Remove unused support/js_patch
* Simplify login_as helper
* Move DatabaseCleaner stuff to its own support file
* Remove unnecessary configuration and requires from spec_helper
---
 .../project/projects_steps.rb                 |  2 +-
 features/support/env.rb                       |  4 +-
 spec/{ => requests}/api/issues_spec.rb        |  0
 spec/{ => requests}/api/projects_spec.rb      |  0
 spec/{ => requests}/api/users_spec.rb         |  0
 spec/spec_helper.rb                           | 37 +++----------------
 spec/support/api.rb                           |  7 ----
 spec/support/api_helpers.rb                   |  9 +++++
 spec/support/db_cleaner.rb                    | 18 +++++++++
 spec/support/js_patch.rb                      |  6 ---
 spec/support/login.rb                         | 30 ---------------
 spec/support/login_helpers.rb                 | 23 ++++++++++++
 spec/{ => support}/monkeypatch.rb             |  0
 spec/support/shared_examples.rb               | 16 --------
 14 files changed, 59 insertions(+), 93 deletions(-)
 rename spec/{ => requests}/api/issues_spec.rb (100%)
 rename spec/{ => requests}/api/projects_spec.rb (100%)
 rename spec/{ => requests}/api/users_spec.rb (100%)
 delete mode 100644 spec/support/api.rb
 create mode 100644 spec/support/api_helpers.rb
 create mode 100644 spec/support/db_cleaner.rb
 delete mode 100644 spec/support/js_patch.rb
 delete mode 100644 spec/support/login.rb
 create mode 100644 spec/support/login_helpers.rb
 rename spec/{ => support}/monkeypatch.rb (100%)
 delete mode 100644 spec/support/shared_examples.rb

diff --git a/features/step_definitions/project/projects_steps.rb b/features/step_definitions/project/projects_steps.rb
index 3ff08d58448..d981e1f3802 100644
--- a/features/step_definitions/project/projects_steps.rb
+++ b/features/step_definitions/project/projects_steps.rb
@@ -1,4 +1,4 @@
-include LoginMacros
+include LoginHelpers
 
 Given /^I signin as a user$/ do
   login_as :user
diff --git a/features/support/env.rb b/features/support/env.rb
index b69a5fe7f07..80a465b472c 100644
--- a/features/support/env.rb
+++ b/features/support/env.rb
@@ -7,9 +7,9 @@ require 'cucumber/rails'
 require 'webmock/cucumber'
 WebMock.allow_net_connect!
 
-require Rails.root.join 'spec/monkeypatch'
 require Rails.root.join 'spec/factories'
-require Rails.root.join 'spec/support/login'
+require Rails.root.join 'spec/support/monkeypatch'
+require Rails.root.join 'spec/support/login_helpers'
 require Rails.root.join 'spec/support/valid_commit'
 
 Capybara.default_selector = :css
diff --git a/spec/api/issues_spec.rb b/spec/requests/api/issues_spec.rb
similarity index 100%
rename from spec/api/issues_spec.rb
rename to spec/requests/api/issues_spec.rb
diff --git a/spec/api/projects_spec.rb b/spec/requests/api/projects_spec.rb
similarity index 100%
rename from spec/api/projects_spec.rb
rename to spec/requests/api/projects_spec.rb
diff --git a/spec/api/users_spec.rb b/spec/requests/api/users_spec.rb
similarity index 100%
rename from spec/api/users_spec.rb
rename to spec/requests/api/users_spec.rb
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index cd931475486..d3635ed1979 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -9,10 +9,8 @@ require File.expand_path("../../config/environment", __FILE__)
 require 'rspec/rails'
 require 'capybara/rails'
 require 'capybara/rspec'
-require 'capybara/dsl'
 require 'webmock/rspec'
 require 'factories'
-require 'monkeypatch'
 require 'email_spec'
 require 'headless'
 
@@ -23,10 +21,13 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
 # Use capybara-webkit
 Capybara.javascript_driver = :webkit
 
+WebMock.disable_net_connect!(allow_localhost: true)
+
 RSpec.configure do |config|
   config.mock_with :rspec
 
-  config.include LoginMacros
+  config.include LoginHelpers, type: :request
+  config.include ApiHelpers,   type: :request
 
   # 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
@@ -38,35 +39,9 @@ RSpec.configure do |config|
     headless.start
   end
 
-  config.before :each, type: :integration do
-    DeviseSessionMock.disable
-  end
-
   config.before do
-    if example.metadata[:js]
-      DatabaseCleaner.strategy = :truncation
-      Capybara::Selenium::Driver::DEFAULT_OPTIONS[:resynchronize] = true
-    else
-      DatabaseCleaner.strategy = :transaction
-    end
-
-    DatabaseCleaner.start
-
-    WebMock.disable_net_connect!(allow_localhost: true)
-
     # !!! Observers disabled by default in tests
-    #
-    #   Use next code to enable observers
-    #   before(:each) { ActiveRecord::Base.observers.enable(:all) }
-    #
-    ActiveRecord::Base.observers.disable :all
-  end
-
-  config.after do
-    DatabaseCleaner.clean
+    ActiveRecord::Base.observers.disable(:all)
+    # ActiveRecord::Base.observers.enable(:all)
   end
-
-  config.include RSpec::Rails::RequestExampleGroup, type: :request, example_group: {
-    file_path: /spec\/api/
-  }
 end
diff --git a/spec/support/api.rb b/spec/support/api.rb
deleted file mode 100644
index d363d8b9a57..00000000000
--- a/spec/support/api.rb
+++ /dev/null
@@ -1,7 +0,0 @@
-def api_prefix
-  "/api/#{Gitlab::API::VERSION}"
-end
-
-def json_response
-  JSON.parse(response.body)
-end
diff --git a/spec/support/api_helpers.rb b/spec/support/api_helpers.rb
new file mode 100644
index 00000000000..e35d5735ec3
--- /dev/null
+++ b/spec/support/api_helpers.rb
@@ -0,0 +1,9 @@
+module ApiHelpers
+  def api_prefix
+    "/api/#{Gitlab::API::VERSION}"
+  end
+
+  def json_response
+    JSON.parse(response.body)
+  end
+end
diff --git a/spec/support/db_cleaner.rb b/spec/support/db_cleaner.rb
new file mode 100644
index 00000000000..f1e072aa15f
--- /dev/null
+++ b/spec/support/db_cleaner.rb
@@ -0,0 +1,18 @@
+require 'database_cleaner'
+
+RSpec.configure do |config|
+  config.before do
+    if example.metadata[:js]
+      DatabaseCleaner.strategy = :truncation
+      Capybara::Selenium::Driver::DEFAULT_OPTIONS[:resynchronize] = true
+    else
+      DatabaseCleaner.strategy = :transaction
+    end
+
+    DatabaseCleaner.start
+  end
+
+  config.after do
+    DatabaseCleaner.clean
+  end
+end
diff --git a/spec/support/js_patch.rb b/spec/support/js_patch.rb
deleted file mode 100644
index 0d4ab264e85..00000000000
--- a/spec/support/js_patch.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-module JsPatch
-  def confirm_js_popup
-    page.evaluate_script("window.alert = function(msg) { return true; }")
-    page.evaluate_script("window.confirm = function(msg) { return true; }")
-  end
-end
diff --git a/spec/support/login.rb b/spec/support/login.rb
deleted file mode 100644
index 78a907bad0b..00000000000
--- a/spec/support/login.rb
+++ /dev/null
@@ -1,30 +0,0 @@
-module LoginMacros
-  def login_as role
-    @user = User.create(email: "user#{User.count}@mail.com",
-                        name: "John Smith",
-                        password: "123456",
-                        password_confirmation: "123456",
-                        skype: 'user_skype')
-
-    if role == :admin
-      @user.admin = true
-      @user.save!
-    end
-
-    visit new_user_session_path
-    fill_in "user_email", with: @user.email
-    fill_in "user_password", with: "123456"
-    click_button "Sign in"
-  end
-
-  def login_with(user)
-    visit new_user_session_path
-    fill_in "user_email", with: user.email
-    fill_in "user_password", with: "123456"
-    click_button "Sign in"
-  end
-
-  def logout
-    click_link "Logout" rescue nil
-  end
-end
diff --git a/spec/support/login_helpers.rb b/spec/support/login_helpers.rb
new file mode 100644
index 00000000000..769034e2286
--- /dev/null
+++ b/spec/support/login_helpers.rb
@@ -0,0 +1,23 @@
+module LoginHelpers
+  # Internal: Create and log in as a user of the specified role
+  #
+  # role - User role (e.g., :admin, :user)
+  def login_as(role)
+    @user = Factory(role)
+    login_with(@user)
+  end
+
+  # Internal: Login as the specified user
+  #
+  # user - User instance to login with
+  def login_with(user)
+    visit new_user_session_path
+    fill_in "user_email", with: user.email
+    fill_in "user_password", with: "123456"
+    click_button "Sign in"
+  end
+
+  def logout
+    click_link "Logout" rescue nil
+  end
+end
diff --git a/spec/monkeypatch.rb b/spec/support/monkeypatch.rb
similarity index 100%
rename from spec/monkeypatch.rb
rename to spec/support/monkeypatch.rb
diff --git a/spec/support/shared_examples.rb b/spec/support/shared_examples.rb
deleted file mode 100644
index 9fd207d0db2..00000000000
--- a/spec/support/shared_examples.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-shared_examples_for :project_side_pane do
-  subject { page }
-  it { should have_content((@project || project).name) }
-  it { should have_content("Commits") }
-  it { should have_content("Files") }
-end
-
-shared_examples_for :tree_view do
-  subject { page }
-
-  it "should have Tree View of project" do
-    should have_content("app")
-    should have_content("History")
-    should have_content("Gemfile")
-  end
-end
-- 
GitLab