Behaviors Guide

Behaviors modify how data is generated, adding delays, failures, and other effects.

Available Behaviors

Latency

Add delays to simulate network or processing time:

User:
  @latency 100ms    # Schema-level: all fields delayed
  id: uuid
  name: name @latency 50ms  # Field-level: specific delay

Arguments:

  • Duration: 100ms, 2s, 500ms, etc.
  • Range: @latency 50ms..200ms (random delay in range)

Failure

Simulate failures with a probability:

User:
  @failure 5%       # 5% chance of failure
  id: uuid
  name: name @failure 1%  # 1% chance for this field

Arguments:

  • Percentage: 5%, 10%, 0.5%

Partial Data

Sometimes omit fields:

User:
  @partial_data 20%  # 20% chance of missing fields
  id: uuid
  name: name
  email: email

Arguments:

  • Percentage: 20%, 10%

Randomize Order

Randomize field generation order:

User:
  @randomize_order
  id: uuid
  name: name
  email: email

Simulate Error

Simulate specific errors:

User:
  @simulate_error StandardError, "Database connection failed"
  id: uuid

Arguments:

  • Error class: StandardError, TimeoutError, etc.
  • Message: Error message string

Close Connection

Simulate connection closure:

User:
  @close_connection
  id: uuid

Behavior Placement

Schema-Level

Apply to all fields in a schema:

User:
  @latency 100ms
  @failure 5%
  id: uuid
  name: name

Field-Level

Apply to specific fields:

User:
  id: uuid @latency 50ms
  name: name @failure 1%
  email: email @partial_data 10%

Combining Behaviors

You can combine multiple behaviors:

User:
  @latency 100ms
  @failure 5%
  id: uuid @latency 50ms  # Field-level overrides schema-level
  name: name @failure 1%

Custom Behaviors

Register custom behaviors:

FakeDataDSL.register_behavior(:my_behavior) do |field, args|
  # Custom behavior logic
end

See Also