Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActionView::TestCase DSL generator #614

Closed
wants to merge 2 commits into from

Conversation

vinistock
Copy link
Member

Closes #570, #494

Motivation

Tests that inherit from ActionView::TestCase have the respective helpers they are testing dynamically included when the test is instantiated.

Basically, this is what Rails does:

# app/helpers/users_helper.rb
module UsersHelper
  def current_user_name
    # ...
  end
end

# test/helpers/users_helper_test.rb
class UsersHelperTest < ActionView::TestCase
  test "current_user_name" do
    # ...
  end
end

# When `UsersHelperTest` is instantiated, Rails dynamically does this
class UsersHelperTest < ActionView::TestCase
  include HelperMethods

  module HelperMethods
    # All available helpers to this test are dynamically included here
    include UsersHelper
  end
end

Implementation

The implementation is straight forward. We gather all tests that inherit from ActionView::TestCase, instantiate the constant with a fake test name to trigger the helpers inclusion and then create the RBI including all of the helper ancestors that were dynamically included.

Concerns

The big challenge for this work is that we currently do not require any test files. In order to actually have this work, we would need to be requiring all test files, which is why I wanted to bring this up for discussion before we move forward.

  • Should we require the test files? We will need a way of preventing the tests from running (i.e.: disable autorun)

Alternative

The alternative implementation would be to not require test files, but to try to figure out the RBIs based on the helpers alone. This also breaks some fundamental assumptions of Tapioca and requires some thought. Namely,

  • We would be using a constant to generate RBIs for a different one (gathering UsersHelper, but generating UsersHelperTest). This requires changes in the DSL generator
  • Another aspect is how to avoid creating unnecessary RBI files. Just because a helper exists, it does mean that its respective ActionView::TestCase test also exists. If we create a test RBI for each helper, we're pretty much guaranteed to generate unnecessary RBIs. We could try to find out whether a test exists, but then that will involve figuring out the right file paths, which is not necessarily enforced (you can put tests for a helper in the wrong path and they'll still run)

Please, let me know your thoughts about the tradeoffs and how you think we should move forward.

Tests

See included tests.

@vinistock vinistock force-pushed the add_action_view_test_case_generator branch from 7091192 to ec23433 Compare November 16, 2021 19:36
@vinistock vinistock requested a review from a team November 16, 2021 19:43
@vinistock vinistock force-pushed the add_action_view_test_case_generator branch from ec23433 to 4464bb3 Compare November 16, 2021 20:27
Copy link
Member

@paracycle paracycle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs some improvements by heavily borrowing from what action_controller_helpers.rb is doing.

@vinistock
Copy link
Member Author

Blocked waiting for #715.

@vinistock
Copy link
Member Author

Closing this PR for now. It might still be beneficial to create this compiler, but there are other steps that need to be addressed first, like how to load test classes confidently.

@vinistock vinistock closed this May 6, 2024
@vinistock vinistock deleted the add_action_view_test_case_generator branch May 6, 2024 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generator for ActionView::TestHelper
2 participants