Best Factory_bot_ruby code snippet using FactoryBot.declare_attribute
factory_spec.rb
Source:factory_spec.rb
...13 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 end165 context "factory name with big letters" do166 subject { FactoryBot::Factory.new(:LoL) }167 its(:names) { should eq [:LoL] }168 its(:human_names) { should eq ["lol"] }169 end170 context "factory name with aliases" do171 subject { FactoryBot::Factory.new(:happy_user, aliases: [:gleeful_user, :person]) }172 its(:names) { should eq [:happy_user, :gleeful_user, :person] }173 its(:human_names) { should eq ["happy user", "gleeful user", "person"] }174 end175end176describe FactoryBot::Factory, "running a factory" do177 subject { FactoryBot::Factory.new(:user) }178 let(:attribute) do179 FactoryBot::Attribute::Dynamic.new(:name, false, -> { "value" })180 end181 let(:declaration) do182 FactoryBot::Declaration::Dynamic.new(:name, false, -> { "value" })183 end184 let(:strategy) { double("strategy", result: "result", add_observer: true) }185 let(:attributes) { [attribute] }186 let(:attribute_list) do187 double("attribute-list", declarations: [declaration], to_a: attributes)188 end189 before do190 define_model("User", name: :string)191 allow(FactoryBot::Declaration::Dynamic).to receive(:new).192 and_return declaration193 allow(declaration).to receive(:to_attributes).and_return attributes194 allow(FactoryBot::Strategy::Build).to receive(:new).and_return strategy195 subject.declare_attribute(declaration)196 end197 it "creates the right strategy using the build class when running" do198 subject.run(FactoryBot::Strategy::Build, {})199 expect(FactoryBot::Strategy::Build).to have_received(:new).once200 end201 it "returns the result from the strategy when running" do202 expect(subject.run(FactoryBot::Strategy::Build, {})).to eq "result"203 end204 it "calls the block and returns the result" do205 block_run = nil206 block = ->(_result) { block_run = "changed" }207 subject.run(FactoryBot::Strategy::Build, {}, &block)208 expect(block_run).to eq "changed"209 end...
declaration_list_spec.rb
Source:declaration_list_spec.rb
...11 declaration2 = double("declaration2", to_attributes: [attribute3])12 attribute_list = double("attribute list", define_attribute: true)13 declaration_list = FactoryBot::DeclarationList.new14 allow(FactoryBot::AttributeList).to receive(:new).and_return attribute_list15 declaration_list.declare_attribute(declaration1)16 declaration_list.declare_attribute(declaration2)17 declaration_list.attributes18 expect(attribute_list).to have_received(:define_attribute).with(attribute1)19 expect(attribute_list).to have_received(:define_attribute).with(attribute2)20 expect(attribute_list).to have_received(:define_attribute).with(attribute3)21 end22end23describe FactoryBot::DeclarationList, "#declare_attribute" do24 it "adds the declaration to the list when not overridable" do25 declaration1 = double("declaration", name: "declaration 1")26 declaration2 = double("declaration", name: "declaration 2")27 declaration_list = FactoryBot::DeclarationList.new28 declaration_list.declare_attribute(declaration1)29 expect(declaration_list.to_a).to eq [declaration1]30 declaration_list.declare_attribute(declaration2)31 expect(declaration_list.to_a).to eq [declaration1, declaration2]32 end33 it "adds the declaration to the list when overridable" do34 declaration1 = double("declaration", name: "declaration 1")35 declaration2 = double("declaration", name: "declaration 2")36 declaration_list = FactoryBot::DeclarationList.new37 declaration_list.overridable38 declaration_list.declare_attribute(declaration1)39 expect(declaration_list.to_a).to eq [declaration1]40 declaration_list.declare_attribute(declaration2)41 expect(declaration_list.to_a).to eq [declaration1, declaration2]42 end43 it "deletes declarations with the same name when overridable" do44 declaration1 = double("declaration", name: "declaration 1")45 declaration2 = double("declaration", name: "declaration 2")46 declaration_with_same_name = double("declaration", name: "declaration 1")47 declaration_list = FactoryBot::DeclarationList.new48 declaration_list.overridable49 declaration_list.declare_attribute(declaration1)50 expect(declaration_list.to_a).to eq [declaration1]51 declaration_list.declare_attribute(declaration2)52 expect(declaration_list.to_a).to eq [declaration1, declaration2]53 declaration_list.declare_attribute(declaration_with_same_name)54 expect(declaration_list.to_a).to eq [declaration2, declaration_with_same_name]55 end56 it "appends declarations with the same name when NOT overridable" do57 declaration1 = double("declaration", name: "declaration 1")58 declaration2 = double("declaration", name: "declaration 2")59 declaration_with_same_name = double("declaration", name: "declaration 1")60 # DeclarationList's `@overridable` attr is set to false by default61 declaration_list = FactoryBot::DeclarationList.new62 declaration_list.declare_attribute(declaration1)63 expect(declaration_list.to_a).to eq [declaration1]64 declaration_list.declare_attribute(declaration2)65 expect(declaration_list.to_a).to eq [declaration1, declaration2]66 declaration_list.declare_attribute(declaration_with_same_name)67 expect(declaration_list.to_a).to eq [declaration1, declaration2, declaration_with_same_name]68 end69end...
declare_attribute
Using AI Code Generation
1FactoryBot.declare_attribute(:user, :name, :string) do2FactoryBot.declare_attribute(:user, :age, :integer) do3FactoryBot.declare_attribute(:user, :email, :string) do
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!!