Last active
August 29, 2015 14:21
-
-
Save backus/847eddd8d3e96d18f76f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'factory_girl' | |
require 'faker' | |
class JsonStrategy # From http://git.io/vT8kC | |
def initialize | |
@strategy = FactoryGirl.strategy_by_name(:create).new | |
end | |
delegate :association, to: :@strategy | |
def result(evaluation) | |
@strategy.result(evaluation).to_json | |
end | |
end | |
FactoryGirl.register_strategy(:json, JsonStrategy) | |
FactoryGirl.define do | |
# Each response has this metadata so we define it as a trait | |
trait :metadata do | |
id { Faker::Base.regexify(/[0-9a-f]{24}/) } | |
created_at { Faker::Time.backward(rand(2..5)).to_i } | |
updated_at { created_at + (60 * 60 * 3) } # created_at + 3 hours | |
note '' | |
end | |
# We don't have create methods since we are creating hashes so we split out | |
# our building instructions into a resource trait | |
trait :resource do | |
skip_create | |
initialize_with { attributes } | |
end | |
# Split out address into a trait | |
trait :address do | |
address_street1 { Faker::Address.street_address } | |
address_street2 do | |
Faker::Address.secondary_address if rand(1..2) == 1 | |
end | |
address_city { Faker::Address.city } | |
address_subdivision { Faker::Address.state_abbr } | |
address_postal_code { Faker::Address.postcode } | |
address_country_code { Faker::Address.country_code } | |
end | |
# Split out name into a trait | |
trait :name do | |
name_first { Faker::Name.first_name } | |
name_middle { nil } | |
name_last { Faker::Name.last_name } | |
end | |
# make livemode and testmode traits | |
trait(:livemode) { livemode true } | |
trait(:testmode) { livemode false } | |
# Composed candidate factory | |
factory :candidate, class: Hash, traits: %i(resource metadata testmode address name) do | |
object 'company' | |
phone_number { Faker::PhoneNumber.phone_number } | |
ssn { Faker::Base.regexify(/\d{4}/) } | |
passport { nil } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> FactoryGirl.create(:candidate) # Create a candidate hash | |
=> { | |
:address_city => "Roobchester", | |
:address_country_code => "LY", | |
:address_postal_code => "23509-8236", | |
:address_street1 => "29942 Will Ville", | |
:address_street2 => "Suite 117", | |
:address_subdivision => "NH", | |
:created_at => 1431914174, | |
:id => "23a0875fcee5767d8fcabe11", | |
:livemode => false, | |
:name_first => "Delaney", | |
:name_last => "Gutmann", | |
:name_middle => nil, | |
:note => "", | |
:object => "company", | |
:passport => nil, | |
:phone_number => "(393) 058-3311", | |
:ssn => "4795", | |
:updated_at => 1431924974 | |
} | |
>> FactoryGirl.create(:candidate, :livemode) # We can specify traits after the name. Livemode will be true here | |
=> { | |
:address_city => "Sipeshaven", | |
:address_country_code => "ML", | |
:address_postal_code => "79744", | |
:address_street1 => "6724 Bo Prairie", | |
:address_street2 => nil, | |
:address_subdivision => "NH", | |
:created_at => 1431965355, | |
:id => "cb762bd4ffeebee88d0dd91f", | |
:livemode => true, | |
:name_first => "Bella", | |
:name_last => "Lesch", | |
:name_middle => nil, | |
:note => "", | |
:object => "company", | |
:passport => nil, | |
:phone_number => "(230) 910-7147 x913", | |
:ssn => "7465", | |
:updated_at => 1431976155 | |
} | |
>> FactoryGirl.json(:candidate) # We can also generate the equivalent JSON | |
=> "{\"id\":\"fb3c5c91263be7f8b8fda77c\",\"created_at\":1431769477,\"updated_at\":1431780277,\"note\":\"\",\"livemode\":false,\"address_street1\":\"44066 Dickens Centers\",\"address_street2\":null,\"address_city\":\"East Clint\",\"address_subdivision\":\"NM\",\"address_postal_code\":\"92701-8573\",\"address_country_code\":\"ZM\",\"name_first\":\"Baron\",\"name_middle\":null,\"name_last\":\"Thiel\",\"object\":\"company\",\"phone_number\":\"993.920.8260\",\"ssn\":\"2160\",\"passport\":null}" | |
>> FactoryGirl.create(:candidate, name_first: "John", name_last: "Backus") # Any specific one off changes can be passed as a hash | |
=> { | |
:address_city => "Keonhaven", | |
:address_country_code => "IE", | |
:address_postal_code => "75323-4298", | |
:address_street1 => "258 Lubowitz Curve", | |
:address_street2 => nil, | |
:address_subdivision => "KS", | |
:created_at => 1431766233, | |
:id => "3c4b1f80dd61bea1cb23a44f", | |
:livemode => false, | |
:name_first => "John", | |
:name_last => "Backus", | |
:name_middle => nil, | |
:note => "", | |
:object => "company", | |
:passport => nil, | |
:phone_number => "(441) 898-5585", | |
:ssn => "0827", | |
:updated_at => 1431777033 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment