Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix issue introduced by YARD's global #log method #1734

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/factory_bot/definition_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down
16 changes: 16 additions & 0 deletions spec/factory_bot/definition_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down