I'm modernizing a gem I wrote years ago called validation_auditor. It has a Rails generator that creates a migration. It's now working well with the latest Rails but I'm having trouble testing it. I have a very simple test:
require_relative "test_helper"
require "generators/validation_auditor/install_generator"
class InstallGeneratorTest < ::Rails::Generators::TestCase
tests ValidationAuditor::InstallGenerator
destination File.expand_path("../tmp", File.dirname(__FILE__))
setup :prepare_destination
test "generate migration" do
run_generator
end
end
and when I run it, it throws this error:
ERROR InstallGeneratorTest#test_generate_migration (0.00s)
Minitest::UnexpectedError: NoMethodError: undefined method `config' for nil:NilClass
/home/pupeno/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/railties-6.1.3/lib/rails.rb:47:in `configuration'
/home/pupeno/validation_auditor/lib/generators/validation_auditor/install_generator.rb:38:in `migration_primary_key_type_string'
(erb):8:in `migration_template'
the error is happening on this line:
active_record = Rails.configuration.generators.active_record
which is part of the installer.
That line is working correctly when I run it in a Rails project, I'm just having trouble making it work in a test.
That line was inspired by Thoughtbot's Clearance, which does successfully test its generator on: https://github.com/thoughtbot/clearance/blob/e653b303e152c8af31a5c30c1b9b13a3e36b3716/spec/generators/clearance/install/install_generator_spec.rb#L9
Any ideas what's the Rails way of doing this? I'm trying to understand how Clearance is working but there's a lot of code to go through and I haven't managed to properly understood yet and I'm wonder if even Clearance is doing it properly.
Aucun commentaire:
Enregistrer un commentaire