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 library aims to provide fixtures for Mongoid the same way you have them with ActiveRecord.
gem 'mongoid-fixture_kit'In your tests, add:
class ActiveSupport::TestCase
include Mongoid::FixtureKit::TestHelper
self.fixture_path = "#{Rails.root}/test/fixtures"
endThis 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, params: { id: @user }
assert_response :success
end
end- Creation of a document from a YAML file
- ERB inside YAML files
$LABELinterpolation in fixture valuesbelongs_torelations- Polymorphic
belongs_to has_manyrelationshas_and_belongs_to_manyrelations- Embedded documents (
embeds_one,embeds_many) DEFAULTSkey for shared fixture attributesTestHelpermodule to include in your tests
You can use ERB in your YAML fixture files to generate dynamic data:
<% 5.times do |i| %>
school<%= i %>:
name: School <%= i %>
<% end %>Use $LABEL in fixture values to reference the fixture's own name:
user_john:
username: $LABEL
email: $LABEL@example.comThis will set username to "user_john" and email to "user_john@example.com".
Use the DEFAULTS key to define shared attributes for all fixtures in a file:
DEFAULTS:
role: member
admin:
name: Admin
role: admin
regular_user:
name: Regular UserThe DEFAULTS entry is removed during loading and is not inserted as a document.
| Dependency | Supported versions |
|---|---|
| Ruby | 3.1, 3.2, 3.3, 3.4 |
| Mongoid | 7.x, 8.x, 9.x |
| Rails (ActiveSupport) | 7.0+ |
Documents are stored with a special attribute __fixture_name which is used to retrieve it and establish relations.
Mongoid::Document has a attr_accessor defined for __fixture_name, so it doesn't pose any problem if you try to dup a document for example.
- There is an option to load fixtures only once.
- Fixture accessor methods are defined publicly.
These 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
endThe original version of this library is mongoid-fixture_set by Geoffroy Planquart in 2014
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.
This project is generously supported by TrophyMap, I18Nature, and several other amazing organizations.