Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
xet7 committed Nov 2, 2024
1 parent 02be5a9 commit 096202c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 49 deletions.
5 changes: 0 additions & 5 deletions test/application_system_test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,4 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
url: "http://selenium:4444",
timeout: 120
}

# Remove duplicate methods since they're now in AuthenticationHelpers
remove_method :sign_in_as if method_defined?(:sign_in_as)
remove_method :current_user if method_defined?(:current_user)
remove_method :teardown if method_defined?(:teardown)
end
27 changes: 27 additions & 0 deletions test/support/authentication_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module AuthenticationHelpers
def sign_in_as(user)
if respond_to?(:visit)
visit sign_in_url
fill_in :email, with: user.email
fill_in :password, with: "Secret1*3*5*"
click_on "Login to your account"
elsif respond_to?(:session)
session[:user_id] = user.id
else
post sign_in_url, params: { email: user.email, password: "Secret1*3*5*" }
end
@current_user = user
end

def current_user
@current_user
end

def teardown
super if defined?(super)
@current_user = nil
session[:user_id] = nil if respond_to?(:session)
end
end
45 changes: 1 addition & 44 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,31 @@
require_relative "../config/environment"
require "rails/test_help"
require "minitest/reporters"
require_relative "support/authentication_helpers"

Minitest::Reporters.use!(
Minitest::Reporters::ProgressReporter.new(color: true),
ENV,
Minitest.backtrace_filter
)

module AuthenticationHelpers
def sign_in_as(user)
if respond_to?(:visit)
visit sign_in_url
fill_in :email, with: user.email
fill_in :password, with: "Secret1*3*5*"
click_on "Login to your account"
elsif respond_to?(:session)
session[:user_id] = user.id
else
post sign_in_url, params: { email: user.email, password: "Secret1*3*5*" }
end
@current_user = user
end

def current_user
@current_user
end

def teardown
super if defined?(super)
@current_user = nil
session[:user_id] = nil if respond_to?(:session)
end
end

module ActiveSupport
class TestCase
include AuthenticationHelpers
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)

# Fix simplecov failing when run in parallel.
# https://github.com/simplecov-ruby/simplecov/issues/718
# If it still has problems then disable parallelize.
# parallelize_setup do |worker|
# SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}"
# end
#
# parallelize_teardown do |_worker|
# SimpleCov.result
# end

# Activate FactoryBot methods
include FactoryBot::Syntax::Methods

# Add more helper methods to be used by all tests here...

# Pundit helpers
def assert_permit(user, record, action)
msg = "User #{user.inspect} should be permitted to #{action} #{record}, but isn't permitted"

assert permit(user, record, action), msg
end

def refute_permit(user, record, action)
msg = "User #{user.inspect} should NOT be permitted to #{action} #{record}, but is permitted"

assert_not permit(user, record, action), msg
end

Expand All @@ -86,9 +46,6 @@ def permit(user, record, action)

class ActionDispatch::IntegrationTest
include AuthenticationHelpers

# remove_method :teardown if method_defined?(:teardown)
remove_method :sign_in_as if method_defined?(:sign_in_as)
end

Shoulda::Matchers.configure do |config|
Expand Down

0 comments on commit 096202c

Please sign in to comment.