diff --git a/lib/factory_bot/definition_proxy.rb b/lib/factory_bot/definition_proxy.rb index a27c33f2c..d8b20e177 100644 --- a/lib/factory_bot/definition_proxy.rb +++ b/lib/factory_bot/definition_proxy.rb @@ -103,6 +103,17 @@ def method_missing(name, *args, &block) # rubocop:disable Style/MissingRespondTo end end + # The YARD gem adds a global `log` method that prevents FactoryBot from being able + # to declare attributes / associations called `log`. + # + # This overrides that method, and instead passes the arguments to `method_missing` so that + # the attribute can be added to the factory. + # + # Source: https://github.com/lsegal/yard/blob/efb33b5411f54ae491c10ce5cd227fa49b985fde/lib/yard/globals.rb#L20-L22 + def log(...) + method_missing(:log, ...) + end + # Adds an attribute that will have unique values generated by a sequence with # a specified format. # diff --git a/spec/factory_bot/definition_proxy_spec.rb b/spec/factory_bot/definition_proxy_spec.rb index 80272bcba..1bd5b214a 100644 --- a/spec/factory_bot/definition_proxy_spec.rb +++ b/spec/factory_bot/definition_proxy_spec.rb @@ -75,6 +75,22 @@ end end +describe FactoryBot::DefinitionProxy, "#log" do + it "ensures that `log` can be declared on the factory" do + begin + Kernel.define_method(:log) { raise "I SHOULDN'T BE CALLED" } + + definition = FactoryBot::Definition.new(:name) + proxy = FactoryBot::DefinitionProxy.new(definition) + proxy.log + + expect(definition).to have_implicit_declaration(:log).with_factory(definition) + ensure + Kernel.send(:remove_method, :log) + end + end +end + describe FactoryBot::DefinitionProxy, "#sequence" do def build_proxy(factory_name) definition = FactoryBot::Definition.new(factory_name)