Back to Repositories

Testing Stubbed Instance Behavior with Custom Primary Keys in Factory Bot

This test suite validates the stubbing functionality in FactoryBot, focusing on how stubbed instances behave with different model configurations and primary key scenarios. It ensures proper simulation of database-persisted records and association handling.

Test Coverage Overview

The test suite comprehensively covers stubbed instance behavior in FactoryBot across multiple scenarios:
  • Basic stubbed instances with associations
  • Stubbed instances with overridden association strategies
  • Models with UUID primary keys
  • Models without primary keys
  • Edge cases like Hash-based models

Implementation Analysis

The testing approach utilizes RSpec’s describe/it blocks to organize test cases logically. It leverages FactoryBot’s build_stubbed method to create test objects that simulate persisted records without touching the database. The implementation demonstrates proper usage of before blocks for setup and custom helper methods for complex scenarios.

Technical Details

Key technical components include:
  • FactoryBot::Syntax::Methods for factory method inclusion
  • ActiveRecord::Base for model definitions
  • Custom define_model helper for dynamic model creation
  • RSpec matchers for expectations
  • Database connection handling for table creation/cleanup

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test cases with proper setup and teardown
  • Clear test descriptions that specify expected behavior
  • Comprehensive coverage of edge cases and variations
  • DRY principle through shared setup and helper methods
  • Proper cleanup of test artifacts

thoughtbot/factory_bot

spec/acceptance/stub_spec.rb

            
describe "a stubbed instance" do
  include FactoryBot::Syntax::Methods

  before do
    define_model("User")

    define_model("Post", user_id: :integer) do
      belongs_to :user
    end

    FactoryBot.define do
      factory :user

      factory :post do
        user
      end
    end
  end

  subject { build_stubbed(:post) }

  it "acts as if it came from the database" do
    should_not be_new_record
  end

  it "assigns associations and acts as if it is saved" do
    expect(subject.user).to be_kind_of(User)
    expect(subject.user).not_to be_new_record
  end
end

describe "a stubbed instance overriding strategy" do
  include FactoryBot::Syntax::Methods

  before do
    define_model("User")
    define_model("Post", user_id: :integer) do
      belongs_to :user
    end

    FactoryBot.define do
      factory :user

      factory :post do
        association(:user, strategy: :build)
      end
    end
  end

  subject { build_stubbed(:post) }

  it "acts as if it is saved in the database" do
    should_not be_new_record
  end

  it "assigns associations and acts as if it is saved" do
    expect(subject.user).to be_kind_of(User)
    expect(subject.user).not_to be_new_record
  end
end

describe "overridden primary keys conventions" do
  describe "a stubbed instance with a uuid primary key" do
    it "builds a stubbed instance" do
      using_model("ModelWithUuid", primary_key: :uuid) do
        FactoryBot.define do
          factory :model_with_uuid
        end

        model = FactoryBot.build_stubbed(:model_with_uuid)
        expect(model).to be_truthy
      end
    end

    it "behaves like a persisted record" do
      using_model("ModelWithUuid", primary_key: :uuid) do
        FactoryBot.define do
          factory :model_with_uuid
        end

        model = FactoryBot.build_stubbed(:model_with_uuid)
        expect(model).to be_persisted
        expect(model).not_to be_new_record
      end
    end

    it "has a uuid primary key" do
      using_model("ModelWithUuid", primary_key: :uuid) do
        FactoryBot.define do
          factory :model_with_uuid
        end

        model = FactoryBot.build_stubbed(:model_with_uuid)
        expect(model.id).to be_a(String)
      end
    end
  end

  describe "a stubbed instance with no primary key" do
    it "builds a stubbed instance" do
      using_model("ModelWithoutPk", primary_key: false) do
        FactoryBot.define do
          factory :model_without_pk
        end

        model = FactoryBot.build_stubbed(:model_without_pk)
        expect(model).to be_truthy
      end
    end

    it "behaves like a persisted record" do
      using_model("ModelWithoutPk", primary_key: false) do
        FactoryBot.define do
          factory :model_without_pk
        end

        model = FactoryBot.build_stubbed(:model_without_pk)
        expect(model).to be_persisted
        expect(model).not_to be_new_record
      end
    end
  end

  describe "a stubbed instance with no id setter" do
    it "builds a stubbed instance" do
      FactoryBot.define do
        factory :model_hash, class: Hash
      end

      model = FactoryBot.build_stubbed(:model_hash)
      expect(model).to be_truthy
    end
  end

  def using_model(name, primary_key:)
    define_class(name, ActiveRecord::Base)

    connection = ActiveRecord::Base.connection
    begin
      clear_generated_table(name.tableize)
      connection.create_table(name.tableize, id: primary_key) do |t|
        t.column :updated_at, :datetime
      end

      yield
    ensure
      clear_generated_table(name.tableize)
    end
  end
end