|
| 1 | +describe FactoryBot::DefinitionHierarchy do |
| 2 | + it "defines a to_create method when the definition has one" do |
| 3 | + definition = double("definition", |
| 4 | + to_create: -> { "custom create" }, |
| 5 | + constructor: nil, |
| 6 | + callbacks: []) |
| 7 | + hierarchy_class = Class.new(FactoryBot::DefinitionHierarchy) |
| 8 | + hierarchy_class.build_from_definition(definition) |
| 9 | + |
| 10 | + expect(hierarchy_class.new.to_create).to eq definition.to_create |
| 11 | + end |
| 12 | + |
| 13 | + it "defines a constructor method when the definition has one" do |
| 14 | + constructor_block = -> { "custom constructor" } |
| 15 | + definition = double("definition", |
| 16 | + to_create: nil, |
| 17 | + constructor: constructor_block, |
| 18 | + callbacks: []) |
| 19 | + hierarchy_class = Class.new(FactoryBot::DefinitionHierarchy) |
| 20 | + hierarchy_class.build_from_definition(definition) |
| 21 | + |
| 22 | + expect(hierarchy_class.new.constructor).to eq constructor_block |
| 23 | + end |
| 24 | + |
| 25 | + it "adds callbacks when the definition has them" do |
| 26 | + callback = double("callback") |
| 27 | + definition = double("definition", |
| 28 | + to_create: nil, |
| 29 | + constructor: nil, |
| 30 | + callbacks: [callback]) |
| 31 | + hierarchy_class = Class.new(FactoryBot::DefinitionHierarchy) |
| 32 | + hierarchy_class.build_from_definition(definition) |
| 33 | + |
| 34 | + expect(hierarchy_class.new.callbacks).to include(callback) |
| 35 | + end |
| 36 | + |
| 37 | + it "falls back to Internal defaults when the definition has no values" do |
| 38 | + definition = double("definition", |
| 39 | + to_create: nil, |
| 40 | + constructor: nil, |
| 41 | + callbacks: []) |
| 42 | + hierarchy_class = Class.new(FactoryBot::DefinitionHierarchy) |
| 43 | + hierarchy_class.build_from_definition(definition) |
| 44 | + |
| 45 | + expect(hierarchy_class.new.callbacks).to eq FactoryBot::Internal.callbacks |
| 46 | + end |
| 47 | +end |
0 commit comments