minitest / minitest-rails

Minitest integration for Rails
http://blowmage.com/minitest-rails
MIT License
540 stars 128 forks source link

Classes outside regular Rails directories do not inherit from `ActiveSupport::TestCase` #233

Closed luizkowalski closed 2 years ago

luizkowalski commented 2 years ago

I have the following configuration for test_helper.rb

# frozen_string_literal: true

ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'minitest/rails'

Dir[Rails.root.join('test/support/**/*.rb')].each { |f| require f }

class ActiveSupport::TestCase
  include FactoryBot::Syntax::Methods
  include CustomAssertions
  include Sugar

  # Run tests in parallel with specified workers
  # parallelize(workers: :number_of_processors)

  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

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

and I have some tests under tests/controller/... and they are working fine. they have access to CustomAssertions and Sugar methods I included

but tests outside regular Rails directories, in my app, test/components/banking/services/bank_information_fetcher_test.rb do no inherit from ActiveSupport::TestCase and they don't have access to these methods.

This is the output of the ancestor list on a controller

[1] pry(#<Api::V1::BusinessesControllerTest>)> self.class.ancestors
=> [Api::V1::BusinessesControllerTest,
 ActionDispatch::IntegrationTest,
 Minitest::Rails::Expectations::ActionDispatch,
 ActiveJob::TestHelper,
 ActionDispatch::IntegrationTest::UrlOptions,
 ActionDispatch::Routing::UrlFor,
 ActionDispatch::Routing::PolymorphicRoutes,
 ActionDispatch::IntegrationTest::Behavior,
 ActionController::TemplateAssertions,
 ActionDispatch::Integration::Runner,
 ActionDispatch::Assertions,
 ActionDispatch::Assertions::RoutingAssertions,
 ActionDispatch::Assertions::ResponseAssertions,
 Rails::Dom::Testing::Assertions,
 Rails::Dom::Testing::Assertions::SelectorAssertions,
 Rails::Dom::Testing::Assertions::SelectorAssertions::CountDescribable,
 Rails::Dom::Testing::Assertions::DomAssertions,
 ActionDispatch::TestProcess::FixtureFile,
 ActiveSupport::Testing::SetupAndTeardown,
 ActiveSupport::TestCase, # <== Here it is
 #<Module:0x00007ff6375736b0>,
 Sugar,
 CustomAssertions, # <== my custom module
 FactoryBot::Syntax::Methods,
 ActiveSupport::Testing::ConstantLookup,
[1] pry(#<Banking::Services::BankInformationFetcher>)> self.class.ancestors
=> [Minitest::Rails::SpecTests::Test__Banking_Services_BankInformationFetcher__test_components_banking_services_bank_information_fetcher_test_rb__5,
 Minitest::Spec,
 Minitest::Spec::DSL::InstanceMethods,
 Minitest::Test,
 Minitest::Guard,
 Minitest::Test::LifecycleHooks,
 Minitest::Reportable,
 Minitest::Assertions,
 Minitest::Runnable,
 ActiveSupport::ForkTracker::CoreExtPrivate,
 ActiveSupport::ForkTracker::CoreExt,
 ActiveSupport::Dependencies::ZeitwerkIntegration::RequireDependency,
 ActiveSupport::ToJsonWithActiveSupportEncoder,
 Object,
 Minitest::Rails::Expectations::ActiveSupport,
 Minitest::Expectations,
 PP::ObjectMixin,
 JSON::Ext::Generator::GeneratorMethods::Object,
 ActiveSupport::Dependencies::Loadable,
 ActiveSupport::Tryable,
 ActiveSupport::ForkTracker::CoreExtPrivate,
 ActiveSupport::ForkTracker::CoreExt,
 Kernel,
 BasicObject]

I'm not sure what is going on...the tests are described in the exact same way:

# frozen_string_literal: true

require 'test_helper'

describe Banking::Services::BankInformationFetcher do
  subject do
    VCR.use_cassette(vcr_cassette) do

and the controller test

# frozen_string_literal: true

require 'test_helper'

describe Api::V1::BanksController do
  describe 'GET info' do
    subject do
      VCR.use_cassette(vcr_cassette) { get(api_v1_banks_info_path(params)) }
    end

have you ever seen something like that? or am I doing something wrong, some config. is missing? let me know if you need more data

Edit: I noticed that if I change the class definition from

describe Banking::Services::BankInformationFetcher do

to

class Banking::Services::BankInformationFetcherTest < ActiveSupport::TestCase

then it works but now our test codebase has some files using class xxx < ActiveSupport::TestCase and others using describe and I would like to stick to one or another, in this case, describe

zenspider commented 2 years ago

Doesn't have Controller in the classname, so it doesn't try to make it a controller test...

I think you can tack on :controller as an argument after the class in your describe... oh, no, I guess it is now :integration.

Look at the register_spec_type calls in lib/minitest/rails.rb for more info.

luizkowalski commented 2 years ago

Doesn't have Controller in the classname, so it doesn't try to make it a controller test...

Looking back now, this makes total sense 🤦🏻 I totally missed this

Thanks for getting back to me, @zenspider but I don't work at the company where I had this issue anymore, unfortunately. I will ping my older team anyway but this can be closed

blowmage commented 2 years ago

If you want to override how Minitest Spec matches the class name passed to describe, you can use the spec DSL's additional descriptors to indicate what test class should be used.

To use ActiveSupport::TestCase call the following:

describe Banking::Services::BankInformationFetcher, :model do

To use ActionDispatch::IntegrationTest call the following:

describe Banking::Services::BankInformationFetcher, :integration do
blowmage commented 2 years ago

If this answers the question plz close the issue. Thanks!

luizkowalski commented 2 years ago

it does! thanks, I'm closing it now