### Run FixtureBot Playground Example Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This command executes the FixtureBot executable to display the contents of the playground blog directory. It requires the 'fixturebot' gem to be installed and available in the bundle. ```bash bundle exec exe/fixturebot show ./playground/blog ``` -------------------------------- ### Rails Installation and Configuration Source: https://context7.com/rubymonolith/fixturebot/llms.txt Instructions for installing FixtureBot in a Rails application using the provided generator or manual setup. Includes adding the gem, running the generator, and manual Rake task compilation. ```bash # Add to Gemfile echo 'gem "fixturebot-rails"' >> Gemfile bundle install # Run the install generator (creates spec/fixtures.rb or test/fixtures.rb) rails generate fixturebot:install # Manual rake task to compile fixtures bundle exec rake fixturebot:compile ``` -------------------------------- ### Manual RSpec and Minitest Setup for FixtureBot Source: https://context7.com/rubymonolith/fixturebot/llms.txt Guides for manually integrating FixtureBot into RSpec and Minitest test suites. Shows how to add the necessary require statements and configure custom fixture file paths and output directories. ```ruby # Manual RSpec setup - add to spec/rails_helper.rb require "fixturebot/rspec" # Fixtures auto-compile before each suite run # Manual Minitest setup - add to test/test_helper.rb require "fixturebot/minitest" # Fixtures auto-compile when helper is loaded # Custom configuration - config/application.rb or config/environments/test.rb config.fixturebot.fixtures_file = "test/my_fixtures.rb" config.fixturebot.output_dir = "test/fixtures" ``` -------------------------------- ### Manual RSpec Setup for FixtureBot Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This Ruby code demonstrates the manual setup required for FixtureBot integration with RSpec. It involves adding a specific require statement to the RSpec configuration file (`spec/rails_helper.rb`) and creating a fixture definition file. ```ruby require "fixturebot/rspec" # Create spec/fixtures.rb with your fixture definitions. ``` -------------------------------- ### Manual Minitest Setup for FixtureBot Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This Ruby code shows the manual steps to integrate FixtureBot with Minitest. It requires adding a specific line to the Minitest helper file (`test/test_helper.rb`) and creating a fixture definition file. ```ruby require "fixturebot/minitest" # Create test/fixtures.rb with your fixture definitions. ``` -------------------------------- ### Install FixtureBot Gem Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This code snippet shows how to add the FixtureBot gem to your Rails project's Gemfile. This is the first step to integrating FixtureBot for generating test fixtures. ```ruby gem "fixturebot-rails" ``` -------------------------------- ### Install FixtureBot with Rails Generator Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This bash command utilizes the Rails generator provided by FixtureBot to set up the necessary files and configurations for using FixtureBot in your project. It typically creates a skeleton DSL file and updates test helpers. ```bash rails generate fixturebot:install ``` -------------------------------- ### Define Named Records with Literal Values (FixtureBot DSL) Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This example shows how to define a named record, `:brad`, using FixtureBot's DSL. It explicitly sets the `name` and `email` columns for the `user` fixture. ```ruby FixtureBot.define do user :brad do name "Brad" email "brad@example.com" end end ``` -------------------------------- ### Use Generated Fixtures in Minitest Tests Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This Minitest example illustrates how to use FixtureBot's generated YAML fixtures within a test case. It verifies the association between a post and its author using Minitest assertions. ```ruby # Minitest class PostTest < ActiveSupport::TestCase def test_belongs_to_author post = posts(:hello_world) assert_equal users(:brad), post.author end end ``` -------------------------------- ### Minitest Model Tests Source: https://context7.com/rubymonolith/fixturebot/llms.txt Example Minitest tests for model associations and generator functionality. These tests verify `belongs_to` and `has_many` relationships, as well as the correctness of generated user emails. ```ruby class PostTest < ActiveSupport::TestCase def test_belongs_to_author post = posts(:hello_world) assert_equal users(:brad), post.author end def test_has_many_tags post = posts(:hello_world) assert_includes post.tags, tags(:ruby) assert_includes post.tags, tags(:rails) end def test_generator_fills_in_email alice = users(:alice) assert_equal "alice@example.com", alice.email end end ``` -------------------------------- ### Define User Email Generator in Ruby Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md Sets a default generator for the 'email' column of a 'user' record. The generator runs for each record that doesn't explicitly set the email, using the fixture key to construct the email address. This requires the FixtureBot gem to be installed and configured. ```ruby FixtureBot.define do user.email { |fixture| "#{fixture.key}@example.com" } end ``` -------------------------------- ### FixtureBot CLI: Compile and Preview Fixtures Source: https://context7.com/rubymonolith/fixturebot/llms.txt Demonstrates the command-line interface for FixtureBot, including compiling fixtures into YAML files and previewing generated YAML without writing to disk. Also shows how to check the tool's version. ```bash # Compile fixtures from a directory containing schema.rb and fixtures.rb # Outputs YAML files to DIR/fixtures/ bundle exec exe/fixturebot compile ./playground/blog # Output: # Compiled fixtures to ./playground/blog/fixtures/ # users.yml (3 records) # posts.yml (2 records) # tags.yml (3 records) # posts_tags.yml (4 records) # comments.yml (3 records) # Preview generated YAML without writing files bundle exec exe/fixturebot show ./playground/blog # Output: # # users.yml # brad: # id: 1234567890 # name: Brad # email: brad@blog.test # alice: # id: 987654321 # name: Alice # email: alice@blog.test # ... # Check version bundle exec exe/fixturebot --version # Output: fixturebot 0.2.0 ``` -------------------------------- ### Programmatic YAML Generation with FixtureBot::Compiler Source: https://context7.com/rubymonolith/fixturebot/llms.txt Illustrates how to use the `FixtureBot::Compiler` class to programmatically convert fixture definitions into YAML files. This includes defining schemas, defining fixtures, and compiling them to a specified directory or as a string. ```ruby require "fixturebot" # Define schema manually schema = FixtureBot::Schema.define do table :users, singular: :user, columns: [:name, :email] table :posts, singular: :post, columns: [:title, :author_id] do belongs_to :author, table: :users end end # Define fixtures with the schema fixture_set = FixtureBot.define(schema) do user.email { |fixture| "#{fixture.key}@test.com" } user :alice do name "Alice" end post :first_post do title "My First Post" author :alice end end # Compile to YAML files in a directory compiler = FixtureBot::Compiler.new(fixture_set) compiler.compile("./tmp/fixtures") # Creates: ./tmp/fixtures/users.yml, ./tmp/fixtures/posts.yml # Get YAML for a single table as a string yaml_output = compiler.compile_table(:users) puts yaml_output # Output: # alice: # id: 123456789 # name: Alice # email: alice@test.com ``` -------------------------------- ### Define Fixtures with Ruby DSL (FixtureBot) Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This snippet demonstrates how to define users, posts, and tags using FixtureBot's Ruby DSL. It shows how generators can fill in default values like email addresses and how associations and join tables can be defined. The DSL allows for both explicit column values and auto-generated ones. ```ruby FixtureBot.define do # Generators fill in required columns so you don't have to repeat yourself. # |fixture| gives you the fixture key; bare methods give column values. user.email { |fixture| "#{fixture.key}@example.com" } user :brad do name "Brad" email "brad@example.com" # overrides the generator end user :alice do name "Alice" # email filled in by generator: "alice@example.com" end user :deactivated do name "Ghost" email nil # explicit nil, generator skipped, email set to null end post :hello_world do title "Hello World" body "Welcome to the blog!" author :brad # sets author_id to brad's stable ID tags :ruby, :rails # creates rows in posts_tags end tag.name { |fixture| fixture.key.to_s.capitalize } tag :ruby # name: "Ruby" tag :rails # name: "Rails" end ``` -------------------------------- ### Configure FixtureBot Output Paths Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This Ruby code snippet illustrates how to configure FixtureBot's behavior by setting custom paths for the fixtures file and the output directory. These configurations can be placed in `config/application.rb` or environment-specific files like `config/environments/test.rb`. ```ruby # config/application.rb or config/environments/test.rb config.fixturebot.fixtures_file = "test/my_fixtures.rb" config.fixturebot.output_dir = "test/fixtures" ``` -------------------------------- ### FixtureBot::Rails.compile for Rails Integration Source: https://context7.com/rubymonolith/fixturebot/llms.txt Details the `FixtureBot::Rails.compile` method for integrating FixtureBot into Rails applications. It automatically compiles fixtures using schema auto-detection and can be configured with explicit file paths. ```ruby # Programmatic compilation in Rails environment require "fixturebot/rails" # Compile using auto-detected paths (test/fixtures.rb or spec/fixtures.rb) FixtureBot::Rails.compile # Compile with explicit paths FixtureBot::Rails.compile( fixtures_file: "custom/fixtures.rb", output_dir: "test/fixtures" ) ``` -------------------------------- ### FixtureBot::Key for Stable ID Generation Source: https://context7.com/rubymonolith/fixturebot/llms.txt Explains the `FixtureBot::Key` module's role in generating deterministic and stable IDs for fixture records. This ensures consistency across test runs and simplifies diffs when fixture definitions change. ```ruby require "fixturebot" # Generate stable IDs for records user_id = FixtureBot::Key.generate(:users, :alice) # => 1945957347 (consistent across runs) post_id = FixtureBot::Key.generate(:posts, :hello_world) # => 876234521 (consistent across runs) # Same inputs always produce same output FixtureBot::Key.generate(:users, :alice) == FixtureBot::Key.generate(:users, :alice) # => true # Different tables or names produce different IDs FixtureBot::Key.generate(:users, :alice) != FixtureBot::Key.generate(:users, :bob) # => true FixtureBot::Key.generate(:users, :alice) != FixtureBot::Key.generate(:posts, :alice) # => true ``` -------------------------------- ### Demonstrate Generator Shadowing with Literal and Nil Values in Ruby Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md Illustrates how explicit values (literal strings or nil) can override or bypass defined generators for a 'user.email' column. It shows a literal email, a record where the generator is used, and a record where 'nil' explicitly sets the email to null, skipping the generator. ```ruby FixtureBot.define do user.email { |fixture| "#{fixture.key}@example.com" } user :brad do name "Brad" email "brad@hey.com" # literal, skips the generator end user :alice do name "Alice" # no email set, generator produces "alice@example.com" end user :deactivated do name "Ghost" email nil # explicit nil, skips the generator, sets email to null end end ``` -------------------------------- ### Compile Fixtures Manually with Rake Task Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This bash command executes the `fixturebot:compile` rake task, allowing for manual control over the generation of fixture YAML files. This is an alternative to the automatic generation that occurs during test runs. ```bash bundle exec rake fixturebot:compile ``` -------------------------------- ### Define Tag Name Generator and Records in Ruby Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md Defines a generator for the 'name' column of 'tag' records and then creates several tag records using this generator. The generator capitalizes the tag's key to set its name. This demonstrates how generators can simplify record creation when common patterns exist. ```ruby FixtureBot.define do tag.name { |fixture| fixture.key.to_s.capitalize } tag :ruby tag :rails tag :testing end ``` -------------------------------- ### Define Post with Author Association in Ruby Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md Creates a 'post' record and associates it with an 'author' record using a symbol reference. FixtureBot automatically resolves the symbol ':brad' to the correct foreign key (author_id) based on the defined schema or conventions. ```ruby FixtureBot.define do post :hello_world do title "Hello World" author :brad # sets author_id to brad's stable ID end end ``` -------------------------------- ### Implicit Style for Defining Records in Ruby Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md Demonstrates the default, implicit style for defining records within FixtureBot. Table methods like 'user' are directly available without needing an explicit receiver, making the DSL concise. ```ruby FixtureBot.define do user :brad do name "Brad" end end ``` -------------------------------- ### Define Post with Multiple Tag Associations in Ruby Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md Creates a 'post' record and associates it with multiple 'tag' records using a comma-separated list of symbols. This is used for HABTM (Has And Belongs To Many) associations, generating the necessary entries in the join table (posts_tags). ```ruby FixtureBot.define do post :hello_world do title "Hello World" tags :ruby, :rails # creates rows in posts_tags end end ``` -------------------------------- ### Define Database Schema for FixtureBot in Ruby Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md Manually defines the database schema for FixtureBot outside of a Rails environment. It specifies tables, their singular names, columns, and associations like 'belongs_to' and 'join_table'. This allows FixtureBot to understand the database structure. ```ruby FixtureBot::Schema.define do table :users, singular: :user, columns: [:name, :email] table :posts, singular: :post, columns: [:title, :body, :author_id] do belongs_to :author, table: :users end table :tags, singular: :tag, columns: [:name] join_table :posts_tags, :posts, :tags end ``` -------------------------------- ### Define Records with Auto-Generated IDs and Defaults (FixtureBot DSL) Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md This snippet demonstrates defining records that receive auto-generated IDs and default values from generators. The `user :alice` record will have its email filled by the `user.email` generator, as it's not explicitly provided. ```ruby FixtureBot.define do user.email { |fixture| "#{fixture.key}@example.com" } user :alice # => { id: , email: "alice@example.com" } end ``` -------------------------------- ### Define Fixtures with Ruby DSL - FixtureBot Source: https://context7.com/rubymonolith/fixturebot/llms.txt Defines fixture data using FixtureBot's Ruby DSL. Supports named records, column values, generators for default values, symbol-referenced associations, and join table relationships. Generators can be defined globally or per-record. ```ruby # spec/fixtures.rb or test/fixtures.rb FixtureBot.define do # Generator: fills in email for every user that doesn't set one # The block receives a fixture object with access to fixture.key (the record's symbol name) user.email { |fixture| "#{fixture.key}@example.com" } # Define a named record with explicit values user :brad do name "Brad" email "brad@hey.com" # overrides the generator end # Record using generator for email (will be "alice@example.com") user :alice do name "Alice" end # Explicit nil shadows the generator (email will be NULL) user :deactivated do name "Ghost" email nil end # Generator for tag names - capitalizes the fixture key tag.name { |fixture| fixture.key.to_s.capitalize } # Records with only generators don't need blocks tag :ruby # name: "Ruby" tag :rails # name: "Rails" tag :testing # name: "Testing" # Associations: author refers to the :brad user record # Join tables: tags creates rows in posts_tags post :hello_world do title "Hello World" body "Welcome to the blog!" author :brad # sets author_id to brad's stable ID tags :ruby, :rails # creates rows in posts_tags join table end # Another post with different associations post :tdd_guide do title "Getting Started with TDD" body "Test-driven development is a practice where you write tests before code." author :alice tags :ruby, :testing end # Comments with belongs_to associations comment :great_post do body "Great post, thanks for sharing!" post :hello_world author :alice end end # Explicit receiver style (useful for editor autocompletion) FixtureBot.define do |t| t.user :charlie do name "Charlie" end end ``` -------------------------------- ### Explicit Style for Defining Records in Ruby Source: https://github.com/rubymonolith/fixturebot/blob/main/README.md Shows the explicit style for defining records in FixtureBot, where a block argument (e.g., 't') is used as the receiver for table methods. This style can improve editor autocompletion and clarity in larger files, though it is functionally equivalent to the implicit style. ```ruby FixtureBot.define do |t| t.user :brad do name "Brad" end end ``` -------------------------------- ### Using Generated Fixtures in Tests - RSpec Source: https://context7.com/rubymonolith/fixturebot/llms.txt Demonstrates how to use FixtureBot-generated YAML fixtures within RSpec tests. Fixtures are loaded once before the test suite, and each test runs within a transaction for isolation. ```ruby # RSpec - spec/models/post_spec.rb RSpec.describe Post, type: :model do fixtures :all it "belongs to an author" do post = posts(:hello_world) expect(post.author).to eq(users(:brad)) end it "has many tags through posts_tags" do post = posts(:hello_world) expect(post.tags).to include(tags(:ruby), tags(:rails)) end it "generates consistent fixture data" do alice = users(:alice) expect(alice.email).to eq("alice@example.com") # from generator end end ``` -------------------------------- ### Manual Schema Definition - FixtureBot::Schema Source: https://context7.com/rubymonolith/fixturebot/llms.txt Manually defines the database schema for FixtureBot, useful outside of Rails or when auto-detection fails. Specifies tables, columns, singular names, belongs_to associations, and HABTM join tables. ```ruby # playground/blog/schema.rb FixtureBot::Schema.define do # Basic table with columns table :users, singular: :user, columns: [:name, :email] # Table with belongs_to association # The belongs_to creates an association method and maps to the foreign key column table :posts, singular: :post, columns: [:title, :body, :author_id] do belongs_to :author, table: :users end # Table with multiple belongs_to associations table :comments, singular: :comment, columns: [:body, :post_id, :author_id] do belongs_to :post, table: :posts belongs_to :author, table: :users end table :tags, singular: :tag, columns: [:name] # Join table for HABTM relationship between posts and tags # Creates posts_tags table with post_id and tag_id columns join_table :posts_tags, :posts, :tags end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.