From d290a2b04085a0a75909449aa3375b304c25d1ae Mon Sep 17 00:00:00 2001 From: hammadxcm Date: Sat, 28 Mar 2026 06:45:15 +0500 Subject: [PATCH] Add unit tests for FactoryBot::StrategySyntaxMethodRegistrar --- .../strategy_syntax_method_registrar_spec.rb | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 spec/factory_bot/strategy_syntax_method_registrar_spec.rb diff --git a/spec/factory_bot/strategy_syntax_method_registrar_spec.rb b/spec/factory_bot/strategy_syntax_method_registrar_spec.rb new file mode 100644 index 000000000..94673a510 --- /dev/null +++ b/spec/factory_bot/strategy_syntax_method_registrar_spec.rb @@ -0,0 +1,51 @@ +describe FactoryBot::StrategySyntaxMethodRegistrar do + it "defines a singular strategy method" do + registrar = FactoryBot::StrategySyntaxMethodRegistrar.new(:test_strategy) + registrar.define_strategy_methods + + expect(FactoryBot::Syntax::Methods.method_defined?(:test_strategy)).to be true + ensure + FactoryBot::Syntax::Methods.undef_method(:test_strategy) if FactoryBot::Syntax::Methods.method_defined?(:test_strategy) + FactoryBot::Syntax::Methods.undef_method(:test_strategy_list) if FactoryBot::Syntax::Methods.method_defined?(:test_strategy_list) + FactoryBot::Syntax::Methods.undef_method(:test_strategy_pair) if FactoryBot::Syntax::Methods.method_defined?(:test_strategy_pair) + end + + it "defines a list strategy method" do + registrar = FactoryBot::StrategySyntaxMethodRegistrar.new(:test_strategy2) + registrar.define_strategy_methods + + expect(FactoryBot::Syntax::Methods.method_defined?(:test_strategy2_list)).to be true + ensure + FactoryBot::Syntax::Methods.undef_method(:test_strategy2) if FactoryBot::Syntax::Methods.method_defined?(:test_strategy2) + FactoryBot::Syntax::Methods.undef_method(:test_strategy2_list) if FactoryBot::Syntax::Methods.method_defined?(:test_strategy2_list) + FactoryBot::Syntax::Methods.undef_method(:test_strategy2_pair) if FactoryBot::Syntax::Methods.method_defined?(:test_strategy2_pair) + end + + it "defines a pair strategy method" do + registrar = FactoryBot::StrategySyntaxMethodRegistrar.new(:test_strategy3) + registrar.define_strategy_methods + + expect(FactoryBot::Syntax::Methods.method_defined?(:test_strategy3_pair)).to be true + ensure + FactoryBot::Syntax::Methods.undef_method(:test_strategy3) if FactoryBot::Syntax::Methods.method_defined?(:test_strategy3) + FactoryBot::Syntax::Methods.undef_method(:test_strategy3_list) if FactoryBot::Syntax::Methods.method_defined?(:test_strategy3_list) + FactoryBot::Syntax::Methods.undef_method(:test_strategy3_pair) if FactoryBot::Syntax::Methods.method_defined?(:test_strategy3_pair) + end + + it "wraps the block with index when arity is 2" do + block = ->(instance, index) { [instance, index] } + wrapped = FactoryBot::StrategySyntaxMethodRegistrar.with_index(block, 5) + + expect(wrapped.call("instance")).to eq ["instance", 5] + end + + it "returns the original block when arity is not 2" do + block = ->(instance) { instance } + + expect(FactoryBot::StrategySyntaxMethodRegistrar.with_index(block, 5)).to eq block + end + + it "returns nil when block is nil" do + expect(FactoryBot::StrategySyntaxMethodRegistrar.with_index(nil, 5)).to be_nil + end +end