Best Factory_bot_ruby code snippet using FactoryBot.build_class
factory_spec.rb
Source:factory_spec.rb
...8 it "has a factory name" do9 expect(@factory.name).to eq @name10 end11 it "has a build class" do12 expect(@factory.build_class).to eq @class13 end14 it "returns associations" do15 factory = FactoryBot::Factory.new(:post)16 FactoryBot::Internal.register_factory(FactoryBot::Factory.new(:admin))17 factory.declare_attribute(FactoryBot::Declaration::Association.new(:author, {}))18 factory.declare_attribute(FactoryBot::Declaration::Association.new(:editor, {}))19 factory.declare_attribute(FactoryBot::Declaration::Implicit.new(:admin, factory))20 factory.associations.each do |association|21 expect(association).to be_association22 end23 expect(factory.associations.to_a.length).to eq 324 end25 it "includes associations from the parent factory" do26 association_on_parent = FactoryBot::Declaration::Association.new(:association_on_parent, {})27 association_on_child = FactoryBot::Declaration::Association.new(:association_on_child, {})28 factory = FactoryBot::Factory.new(:post)29 factory.declare_attribute(association_on_parent)30 FactoryBot::Internal.register_factory(factory)31 child_factory = FactoryBot::Factory.new(:child_post, parent: :post)32 child_factory.declare_attribute(association_on_child)33 expect(child_factory.associations.map(&:name)).to eq [:association_on_parent, :association_on_child]34 end35 describe "when overriding generated attributes with a hash" do36 before do37 @name = :name38 @value = "The price is right!"39 @hash = { @name => @value }40 end41 it "returns the overridden value in the generated attributes" do42 declaration =43 FactoryBot::Declaration::Dynamic.new(@name, false, -> { flunk })44 @factory.declare_attribute(declaration)45 result = @factory.run(FactoryBot::Strategy::AttributesFor, @hash)46 expect(result[@name]).to eq @value47 end48 it "overrides a symbol parameter with a string parameter" do49 declaration =50 FactoryBot::Declaration::Dynamic.new(@name, false, -> { flunk })51 @factory.declare_attribute(declaration)52 @hash = { @name.to_s => @value }53 result = @factory.run(FactoryBot::Strategy::AttributesFor, @hash)54 expect(result[@name]).to eq @value55 end56 end57 describe "overriding an attribute with an alias" do58 before do59 attribute = FactoryBot::Declaration::Dynamic.new(60 :test,61 false, -> { "original" }62 )63 @factory.declare_attribute(attribute)64 FactoryBot.aliases << [/(.*)_alias/, '\1']65 @result = @factory.run(66 FactoryBot::Strategy::AttributesFor,67 test_alias: "new",68 )69 end70 it "uses the passed in value for the alias" do71 expect(@result[:test_alias]).to eq "new"72 end73 it "discards the predefined value for the attribute" do74 expect(@result[:test]).to be_nil75 end76 end77 it "guesses the build class from the factory name" do78 expect(@factory.build_class).to eq User79 end80 it "creates a new factory using the class of the parent" do81 child = FactoryBot::Factory.new(:child, parent: @factory.name)82 child.compile83 expect(child.build_class).to eq @factory.build_class84 end85 it "creates a new factory while overriding the parent class" do86 child = FactoryBot::Factory.new(:child, class: String, parent: @factory.name)87 child.compile88 expect(child.build_class).to eq String89 end90end91describe FactoryBot::Factory, "when defined with a custom class" do92 subject { FactoryBot::Factory.new(:author, class: Float) }93 its(:build_class) { should eq Float }94end95describe FactoryBot::Factory, "when given a class that overrides #to_s" do96 let(:overriding_class) { Overriding::Class }97 before do98 define_class("Overriding")99 define_class("Overriding::Class") do100 def self.to_s101 "Overriding"102 end103 end104 end105 subject { FactoryBot::Factory.new(:overriding_class, class: Overriding::Class) }106 it "sets build_class correctly" do107 expect(subject.build_class).to eq overriding_class108 end109end110describe FactoryBot::Factory, "when defined with a class instead of a name" do111 let(:factory_class) { ArgumentError }112 let(:name) { :argument_error }113 subject { FactoryBot::Factory.new(factory_class) }114 its(:name) { should eq name }115 its(:build_class) { should eq factory_class }116end117describe FactoryBot::Factory, "when defined with a custom class name" do118 subject { FactoryBot::Factory.new(:author, class: :argument_error) }119 its(:build_class) { should eq ArgumentError }120end121describe FactoryBot::Factory, "with a name ending in s" do122 let(:name) { :business }123 let(:business_class) { Business }124 before { define_class("Business") }125 subject { FactoryBot::Factory.new(name) }126 its(:name) { should eq name }127 its(:build_class) { should eq business_class }128end129describe FactoryBot::Factory, "with a string for a name" do130 let(:name) { :string }131 subject { FactoryBot::Factory.new(name.to_s) }132 its(:name) { should eq name }133end134describe FactoryBot::Factory, "for namespaced class" do135 let(:name) { :settings }136 let(:settings_class) { Admin::Settings }137 before do138 define_class("Admin")139 define_class("Admin::Settings")140 end141 context "with a namespaced class with Namespace::Class syntax" do142 subject { FactoryBot::Factory.new(name, class: "Admin::Settings") }143 it "sets build_class correctly" do144 expect(subject.build_class).to eq settings_class145 end146 end147 context "with a namespaced class with namespace/class syntax" do148 subject { FactoryBot::Factory.new(name, class: "admin/settings") }149 it "sets build_class correctly" do150 expect(subject.build_class).to eq settings_class151 end152 end153end154describe FactoryBot::Factory, "human names" do155 context "factory name without underscores" do156 subject { FactoryBot::Factory.new(:user) }157 its(:names) { should eq [:user] }158 its(:human_names) { should eq ["user"] }159 end160 context "factory name with underscores" do161 subject { FactoryBot::Factory.new(:happy_user) }162 its(:names) { should eq [:happy_user] }163 its(:human_names) { should eq ["happy user"] }164 end...
factory.rb
Source:factory.rb
...14 @compiled = false15 end16 delegate :add_callback, :declare_attribute, :to_create, :define_trait, :constructor,17 :defined_traits, :inherit_traits, :append_traits, to: :@definition18 def build_class19 @build_class ||= if class_name.is_a? Class20 class_name21 else22 class_name.to_s.camelize.constantize23 end24 end25 def run(build_strategy, overrides, &block)26 block ||= ->(result) { result }27 compile28 strategy = StrategyCalculator.new(build_strategy).strategy.new29 evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)30 attribute_assigner = AttributeAssigner.new(evaluator, build_class, &compiled_constructor)31 evaluation =32 Evaluation.new(evaluator, attribute_assigner, compiled_to_create)33 evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))34 strategy.result(evaluation).tap(&block)35 end36 def human_names37 names.map { |name| name.to_s.humanize.downcase }38 end39 def associations40 evaluator_class.attribute_list.associations41 end42 # Names for this factory, including aliases.43 #44 # Example:45 #46 # factory :user, aliases: [:author] do47 # # ...48 # end49 #50 # FactoryBot.create(:author).class51 # # => User52 #53 # Because an attribute defined without a value or block will build an54 # association with the same name, this allows associations to be defined55 # without factories, such as:56 #57 # factory :user, aliases: [:author] do58 # # ...59 # end60 #61 # factory :post do62 # author63 # end64 #65 # FactoryBot.create(:post).author.class66 # # => User67 def names68 [name] + @aliases69 end70 def compile71 unless @compiled72 parent.compile73 parent.defined_traits.each { |trait| define_trait(trait) }74 @definition.compile(build_class)75 build_hierarchy76 @compiled = true77 end78 end79 def with_traits(traits)80 clone.tap do |factory_with_traits|81 factory_with_traits.append_traits traits82 end83 end84 protected85 def class_name86 @class_name || parent.class_name || name87 end88 def evaluator_class...
find_or_create_strategy.rb
Source:find_or_create_strategy.rb
...5 def association(runner)6 runner.run7 end8 def result(evaluation)9 build_class(evaluation).where(get_match_attributes(evaluation)).first10 end11 private12 def build_class(evaluation)13 @build_class ||= evaluation14 .instance_variable_get(:@attribute_assigner)15 .instance_variable_get(:@build_class)16 end17 def get_match_attributes(evaluation)18 evaluation.hash.keep_if do |attr, _value|19 attr.to_s.in? build_class(evaluation).column_names20 end21 end22 def get_overrides(evaluation = nil)23 evaluation.object24 return @overrides unless @overrides.nil?25 evaluation.instance_variable_get(:@attribute_assigner)26 .instance_variable_get(:@evaluator)27 .instance_variable_get(:@overrides)28 .clone29 end30 end31 class FindOrCreate32 def initialize33 @strategy = FactoryBot.strategy_by_name(:find).new...
build_class
Using AI Code Generation
1 name { Faker::Name.name }2FactoryBot.build_class(:user)3 name { Faker::Book.title }4FactoryBot.build_class(:book)5 name { Faker::Movie.title }6FactoryBot.build_class(:movie)7 name { Faker::Vehicle.make }8FactoryBot.build_class(:car)9 name { Faker::Vehicle.make }10FactoryBot.build_class(:bike)11 name { Faker::Vehicle.make }12FactoryBot.build_class(:laptop)13 name { Faker::Vehicle.make }14FactoryBot.build_class(:mobile)15 name { Faker::Vehicle.make }16FactoryBot.build_class(:pen)17 name { Faker::Vehicle.make }
build_class
Using AI Code Generation
1 build_class(Animal)2 build_class(Animal)3 build_class(Animal)4 build_class(Animal)5 build_class(Animal)6 build_class(Animal)7 build_class(Animal)8 build_class(Animal)9 build_class(Animal)
build_class
Using AI Code Generation
1FactoryBot.build_class(:user) do2FactoryBot.build_class(:user) do3FactoryBot.build_class(:user) do4FactoryBot.build_class(:user) do5FactoryBot.build_class(:user) do6FactoryBot.build_class(:user) do7FactoryBot.build_class(:user) do8FactoryBot.build_class(:user) do
build_class
Using AI Code Generation
1 def self.build_class(klass)2 sequence(:id)3 File.open(factory_file, "w") do |f|4 f.write(factory_code)5 sequence(:id)6 sequence(:id)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!