From eda139b046889f83bbf466cf198c73c11f08ae89 Mon Sep 17 00:00:00 2001 From: Bjorn Forsberg Date: Sun, 20 Aug 2023 18:13:28 +0200 Subject: [PATCH] Yaaay, we have a working test --- app/controllers/metrics_controller.rb | 9 +++++++-- config/environments/test.rb | 1 + test/controllers/home_controller_test.rb | 4 +--- test/controllers/metrics_controller_test.rb | 9 ++++++--- test/fixtures/users.yml | 8 +++----- test/support/rails_4_thread_error_fix.rb | 14 ++++++++++++++ test/test_helper.rb | 8 ++++++++ 7 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 test/support/rails_4_thread_error_fix.rb diff --git a/app/controllers/metrics_controller.rb b/app/controllers/metrics_controller.rb index 5d6028b..dca4658 100644 --- a/app/controllers/metrics_controller.rb +++ b/app/controllers/metrics_controller.rb @@ -21,8 +21,13 @@ def set_data end def set_dates - @first_metric_date = current_user.oldest_metric_date - @latest_metric_date = current_user.newest_metric_date + if current_user.metrics.any? + @first_metric_date = current_user.oldest_metric_date + @latest_metric_date = current_user.newest_metric_date + else + @first_metric_date = Date.today + @latest_metric_date = Date.today + end end def set_app_titles diff --git a/config/environments/test.rb b/config/environments/test.rb index 35b2e05..82725b9 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -36,4 +36,5 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + config.active_support.test_order = :random end diff --git a/test/controllers/home_controller_test.rb b/test/controllers/home_controller_test.rb index 48ed561..9df3bd1 100644 --- a/test/controllers/home_controller_test.rb +++ b/test/controllers/home_controller_test.rb @@ -1,7 +1,5 @@ require "test_helper" class HomeControllerTest < ActionController::TestCase - # test "the truth" do - # assert true - # end + end diff --git a/test/controllers/metrics_controller_test.rb b/test/controllers/metrics_controller_test.rb index 02f222f..46369e5 100644 --- a/test/controllers/metrics_controller_test.rb +++ b/test/controllers/metrics_controller_test.rb @@ -1,7 +1,10 @@ require "test_helper" class MetricsControllerTest < ActionController::TestCase - # test "the truth" do - # assert true - # end + test "should get index" do + sign_in users(:regular) + + get :index + assert_response :success + end end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 937a0c0..1fc2483 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -4,8 +4,6 @@ # model remove the '{}' from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: {} -# column: value -# -two: {} -# column: value +regular: + email: test@example.com + encrypted_password: <%= User.new.send(:password_digest, '123greetings') %> diff --git a/test/support/rails_4_thread_error_fix.rb b/test/support/rails_4_thread_error_fix.rb new file mode 100644 index 0000000..7ca4e0c --- /dev/null +++ b/test/support/rails_4_thread_error_fix.rb @@ -0,0 +1,14 @@ +if RUBY_VERSION>='2.6.0' + if Rails.version < '5' + class ActionController::TestResponse < ActionDispatch::TestResponse + def recycle! + # hack to avoid MonitorMixin double-initialize error: + @mon_mutex_owner_object_id = nil + @mon_mutex = nil + initialize + end + end + else + puts "Monkeypatch for ActionController::TestResponse no longer needed" + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index beaba26..384d88a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,6 +2,9 @@ require File.expand_path("../../config/environment", __FILE__) require "rails/test_help" +# TODO: Remove this when Rails 4 is no longer supported. +require_relative "./support/rails_4_thread_error_fix" + class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. # @@ -10,4 +13,9 @@ class ActiveSupport::TestCase fixtures :all # Add more helper methods to be used by all tests here... + +end + +class ActionController::TestCase + include Devise::TestHelpers end