Back to Repositories

Testing Dynamic Declaration Equality Comparison in FactoryBot

This test suite examines the equality comparison functionality of Dynamic Declarations in FactoryBot. It verifies various scenarios where Dynamic declarations should or should not be considered equal based on their attributes, blocks, and ignored status. The tests ensure robust object comparison behavior essential for factory definitions.

Test Coverage Overview

The test suite provides comprehensive coverage of equality comparison scenarios for FactoryBot’s Dynamic declarations.

Key areas tested include:
  • Identical attribute comparison
  • Different name comparison
  • Different block comparison
  • Ignored status comparison
  • Cross-type object comparison

Implementation Analysis

The testing approach uses RSpec’s context-based structure to organize different equality comparison scenarios. The implementation leverages lambda blocks and boolean flags to test various declaration states, demonstrating both positive and negative equality assertions using RSpec’s expectation syntax.

Technical patterns include:
  • Lambda block usage for dynamic content
  • Boolean flag testing for ignored status
  • Symbol-based naming comparisons

Technical Details

Testing tools and configuration:
  • RSpec as the testing framework
  • FactoryBot’s Declaration::Dynamic class
  • Lambda blocks for dynamic content generation
  • Boolean flags for state management
  • Symbol identifiers for declaration names

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby and RSpec.

Notable practices include:
  • Descriptive context blocks for different scenarios
  • Isolated test cases for each comparison type
  • Clear expectation statements
  • Consistent variable naming
  • Proper setup of test objects

thoughtbot/factory_bot

spec/factory_bot/declaration/dynamic_spec.rb

            
describe FactoryBot::Declaration::Dynamic do
  describe "#==" do
    context "when the attributes are equal" do
      it "the objects are equal" do
        block = -> {}
        declaration = described_class.new(:name, false, block)
        other_declaration = described_class.new(:name, false, block)

        expect(declaration).to eq(other_declaration)
      end
    end

    context "when the names are different" do
      it "the objects are NOT equal" do
        block = -> {}
        declaration = described_class.new(:name, false, block)
        other_declaration = described_class.new(:other_name, false, block)

        expect(declaration).not_to eq(other_declaration)
      end
    end

    context "when the blocks are different" do
      it "the objects are NOT equal" do
        declaration = described_class.new(:name, false, -> {})
        other_declaration = described_class.new(:name, false, -> {})

        expect(declaration).not_to eq(other_declaration)
      end
    end

    context "when one is ignored and the other isn't" do
      it "the objects are NOT equal" do
        block = -> {}
        declaration = described_class.new(:name, false, block)
        other_declaration = described_class.new(:name, true, block)

        expect(declaration).not_to eq(other_declaration)
      end
    end

    context "when comparing against another type of object" do
      it "the objects are NOT equal" do
        declaration = described_class.new(:name, false, -> {})

        expect(declaration).not_to eq(:not_a_declaration)
      end
    end
  end
end