ekampp / mongoid-fixture-kit

This package is a Ruby gem that provides a way to load sample data into a MongoDB database for testing purposes. It provides a simple and convenient way to manage test data by defining fixtures in YAML files, which can be loaded into the database before running tests.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mongoid-fixture_kit

Version Download License

Buy Me A Coffee

This package is a Ruby gem that provides a way to load sample data into a MongoDB database for testing purposes. It provides a simple and convenient way to manage test data by defining fixtures in YAML files, which can be loaded into the database before running tests.

This ruby gem aims to provide fixtures for Mongoid the same way you have them with ActiveRecord.

Install

gem 'mongoid-fixture_kit'

How to use

In your tests, add:

class ActiveSupport::TestCase
  include Mongoid::FixtureKit::TestHelper
  self.fixture_path = "#{Rails.root}/test/fixtures"
end

This is also done by ActiveRecord, but magically in the railties.

Then when you want to access a fixture:

class UsersControllerTest < ActionController::TestCase
  setup do
    @user = users(:user_1)
  end
  
  test 'should show user' do
    get :show, id: @user
    assert_response :success
  end
end

Features

  • Creation of a document from an YAML file.
  • belongs_to relations
  • ERB inside YAML files
  • YAML DEFAULTS feature
  • Polymorphic belongs_to
  • has_many relations
  • has_and_belongs_to_many relations
  • TestHelper module to include in your tests

Notes

Original fixtures from ActiveRecord also uses a selection based on class_names for which I haven't seen any use case, so I did not port this feature yet.

I did not find how ActiveRecord::TestFixtures defines its fixture_table_names so I'm simply searching for all YAML files under self.fixture_path, which is enough for what I want.

Array attributes are receiving a special treatment, i.e. they are joined with new values, not replaced by the new one. This is used for has_and_belongs_to_many relations.

Documents are stored with a special attribute __fixture_name which is used to retrieve it and establish relations.

Mongoid::Document has an attr_accessor defined for __fixture_name so it doesn't pose any problem if you try to dup a document for example.

Changes compared to ActiveRecord

  • There is an option to load fixtures only once.
  • Fixture accessor methods are defined publicly.

This changes are here to let you create another class holding persistent data inside your tests.

class TestData
  include Mongoid::FixtureKit::TestHelper

  self.fixture_path = "#{Rails.root}/test/fixtures_universes"
  self.load_fixtures_once = true

  def TestData.instance
    @instance ||= ->(){
      instance = new
      instance.setup_fixtures
      instance
    }.call
  end

  private_class_method :new
end

class ActiveSupport::TestCase
  include Mongoid::FixtureKit::TestHelper
  self.fixture_path = "#{Rails.root}/test/fixtures"

  def data
    TestData.instance
  end
end

# somewhere else
test 'should validate complex data structure' do
  assert_nothing_raised do
    DataStructure.process(data.structures(:complex))
  end
end

License

The original version of this library is mongoid-fixture_set by Geoffroy Planquart in 2014

Bugs or Requests

If you encounter any problems feel free to open an issue. If you feel the library is missing a feature, please raise a ticket. Pull request are also welcome.

DigitalOcean Referral Badge

Developer

Dániel Sipos

Sponsors

This project is generously supported by TrophyMap, I18Nature, and several other amazing organizations.

About

This package is a Ruby gem that provides a way to load sample data into a MongoDB database for testing purposes. It provides a simple and convenient way to manage test data by defining fixtures in YAML files, which can be loaded into the database before running tests.

License:MIT License


Languages

Language:Ruby 100.0%