Module: Servactory::TestKit::Utils::Faker
Overview
Generates fake test values for different Ruby types.
## Purpose
Provides fake values for testing input validations. Used by ValidWithSubmatcher to generate values that will trigger specific validation failures (e.g., wrong inclusion values).
## Usage
“‘ruby # Get a fake value for a type Faker.fetch_value_for(String) # => “fake” Faker.fetch_value_for(Integer) # => 123 Faker.fetch_value_for(Array, of: :integer) # => [1, 2, 3] “`
## Supported Types
-
Symbol - returns ‘:fake`
-
String - returns ‘“fake”`
-
Integer - returns ‘123`
-
Float - returns ‘12.3`
-
Range - returns ‘1..3`
-
Array - returns ‘[“fake”]` or `[1, 2, 3]` based on `of:` param
-
Hash - returns ‘{ fake: :yes }` or `{ fake: 1 }` based on `of:` param
-
TrueClass - returns ‘true`
-
FalseClass - returns ‘false`
-
NilClass - returns ‘nil`
Instance Method Summary collapse
-
#fetch_value_for(class_or_name, of: :string) ⇒ Object
Generates a fake value for the given type.
Instance Method Details
#fetch_value_for(class_or_name, of: :string) ⇒ Object
Generates a fake value for the given type.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/servactory/test_kit/utils/faker.rb', line 43 def fetch_value_for(class_or_name, of: :string) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize class_for_fake = Servactory::Utils.constantize_class(class_or_name) of = of.to_sym faker = { Symbol => method(:fake_symbol), String => method(:fake_string), Integer => method(:fake_integer), Float => method(:fake_float), Range => method(:fake_range), Array => -> { fake_array(of) }, Hash => -> { fake_hash(of) }, TrueClass => method(:fake_true_class), FalseClass => method(:fake_false_class), NilClass => method(:fake_nil_class) } fake_class = faker[class_for_fake] || -> { unsupported_faker(class_for_fake) } fake_class.call end |