0.0
No commit activity in last 3 years
No release in over 3 years
Dynamically creating ActiveRecord Fixtures.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 4.0
 Project Readme

FixtureBox

Dynamically creating ActiveRecord Fixtures.

Usage

class Book < ApplicationRecord
  belongs_to :author
  has_many :files, class_name: 'Book::File'
end

class Author < ApplicationRecord
  belongs_to :book
end

class Book::File < ApplicationRecord
  belongs_to :book
end

fixture_data = {
  books: {
    wagahai_wa_neko_de_aru: {
      name: 'Wagahai wa Neko de Aru',
      author: :natsume_soseki
    },
    ame_ni_mo_makezu: {
      name: 'Ame ni mo Makezu',
      author: :miyazawa_kenji
    }
  },
  authors: {
    natsume_soseki: {
      name: 'Natsume Soseki'
    },
    miyazawa_kenji: {
      name: 'Miyazawa Kenji'
    }
  },
  book_files: {
    wagahai_wa_neko_de_aru_zip: {
      book: :wagahai_wa_neko_de_aru,
      content_type: 'application/zip'
    },
    wagahai_wa_neko_de_aru_ebk: {
      book: :wagahai_wa_neko_de_aru,
      content_type: 'application/x-expandedbook'
    }
  }
}
class_names = {
  book_files: Book::File
}

fixture_box = FixtureBox.new(fixture_data, class_names)

assert_equal 'Wagahai wa Neko de Aru', @fixture_box.books(:wagahai_wa_neko_de_aru).name

# belongs_to
assert_equal 'Natsume Soseki', @fixture_box.books(:wagahai_wa_neko_de_aru).author.name

# Set the model class
assert_equal 'application/zip', @fixture_box.book_files(:wagahai_wa_neko_de_aru_zip).content_type 
assert_equal %w(application/zip application/x-expandedbook), @fixture_box.books(:wagahai_wa_neko_de_aru).files.pluck(:content_type)

# Get all instances
assert_equal ['Wagahai wa Neko de Aru', 'Ame ni mo Makezu'], @fixture_box.books.map(&:name)

See more at test/fixture_box_test.rb

Installation

Add this line to your application's Gemfile:

gem 'fixture_box'

And then execute:

$ bundle

Or install it yourself as:

$ gem install fixture_box

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.