FactoryJson

Hi! The main purpose of this gem is to provide a simple way to create JSON data structures using the power of FactoryGirl's DSL.

Installation

Add this line to your application's Gemfile:

gem 'factory_json'

And then execute:

$ bundle

Or install it yourself as:

$ gem install factory_json

Usage

This extension provides two new ways of working with FactoryGirl

Dealing with Hashes

Tired of creating json fixtures somewhere in your app and keeping them up to date alongside with your factories? Now you can create your JSON fixtures with factories: All you need to do is specify a Hash as the build class while registering the factory ... and that is pretty much it!

# spec/factories/data_structures.rb
FactoryGirl.define do
  factory :entry, class: Hash do
    name "This one is special"
    location "The place where you can always see a Sunrise"

    after(:build) do |entry|
      entry[:relations] = FactoryGirl.build_list(entry_relation, 3, :with_address)
    end
  end

  factory :entry_relation, class: Hash do
    type "RELATED"
    label "Apartment:"
    name_summary "Mr Darth Vader"

    trait :with_address do
      street_summary "Tatooine"
      city_summary "Galactic Empire"
    end
  end
end

And this might be a way to use these in our specs

entry_relation = FactoryGirl.as_json(:entry_relation, :with_address) # to return data as a Ruby hash, that beeing piped through JSON parse
entry_relation = FactoryGirl.json(:entry_relation, :with_address) # to return string, that contains valid JSON

Dealing with Models

You have a factory that you'd also like to use as a JSON fixture? Use common usage patterns (to_json) with our fixtures now! Not only can you get JSON from your factories, but of course also Hashes. All you need to do is to define to_json or to_hash (since to_json is based on to_hash) on your model.

# app/models.rb
class User

  has_one :profile

  def to_hash
    {
      name: self.name,
      email: self.email,
      profile: profile.to_hash
    }.to_json
  end

  def to_json(*args)
    to_hash.to_json(*args)
  end
end

class Profile

  belongs_to :user

  def to_hash
    {
      age: self.age,
      hair_color: self.hair_color,
      child_dream: self.child_dream,
      i_will_start_running: "tomorrow"
    }
  end

  def to_json(*args)
    to_hash.to_json(*args)
  end
end

# spec/factories.rb
FactoryGirl.define do
  factory :user do
    name "User 1"
    email "[email protected]"

    trait :with_profile
      association :profile
    end
  end

  factory :profile do
    age "18 years"
    hair_color "blue"
    child_dream "To become a Jedi Knight"
  end
end
user = FactoryGirl.build(:user, :with_profile) # to return new User object
user = FactoryGirl.create(:user, :with_profile) # to return persistent User object
user_hash = FactoryGirl.as_json(:user, :with_profile) # to return data as a Ruby hash, that beeing piped through JSON parse
user_json = FactoryGirl.json(:user, :with_profile) # to return string, that contains valid JSON

Testing

Testing is pretty much standard

rspec spec

Testing compatibility with FactoryGirl

rspec `bundle show factory_girl`/spec

Contributing

  1. Fork it ( https://github.com/[my-github-username]/factory_json/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request