Skip to content
Closed
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
51 changes: 51 additions & 0 deletions spec/factory_bot/strategy_syntax_method_registrar_spec.rb
Original file line number Diff line number Diff line change
@@ -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