All Projects → fnando → factory_bot-preload

fnando / factory_bot-preload

Licence: MIT License
Preload factories (factory_bot) just like fixtures. It will be easy and, probably, faster!

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to factory bot-preload

Email Spec
Collection of RSpec/MiniTest matchers and Cucumber steps for testing email in a ruby app using ActionMailer or Pony
Stars: ✭ 1,142 (+1579.41%)
Mutual labels:  rspec, minitest
Mutant
Automated code reviews via mutation testing - semantic code coverage.
Stars: ✭ 1,794 (+2538.24%)
Mutual labels:  rspec, minitest
Fake ftp
A fake FTP server for use with ruby tests
Stars: ✭ 77 (+13.24%)
Mutual labels:  rspec, minitest
Knapsack
Knapsack splits tests evenly across parallel CI nodes to run fast CI build and save you time.
Stars: ✭ 430 (+532.35%)
Mutual labels:  rspec, minitest
Rantly
Ruby Imperative Random Data Generator and Quickcheck
Stars: ✭ 241 (+254.41%)
Mutual labels:  rspec, minitest
Vscode Ruby Test Adapter
A Ruby test adapter extension for the VS Code Test Explorer
Stars: ✭ 50 (-26.47%)
Mutual labels:  rspec, minitest
Lurker
📖 The ultimate tool for documenting and testing APIs in Rails
Stars: ✭ 120 (+76.47%)
Mutual labels:  rspec, minitest
Aruba
Test command-line applications with Cucumber-Ruby, RSpec or Minitest. The most up to date documentation can be found on Cucumber.Pro (https://app.cucumber.pro/projects/aruba)
Stars: ✭ 900 (+1223.53%)
Mutual labels:  rspec, minitest
Still life
Rails upgrade's best friend
Stars: ✭ 213 (+213.24%)
Mutual labels:  rspec, minitest
Action Cable Testing
Action Cable testing utils
Stars: ✭ 192 (+182.35%)
Mutual labels:  rspec, minitest
N plus one control
RSpec and Minitest matchers to prevent N+1 queries problem
Stars: ✭ 345 (+407.35%)
Mutual labels:  rspec, minitest
capybara select2
Capybara helpers for https://select2.org select box (supports Select2 version 2/3/4)
Stars: ✭ 48 (-29.41%)
Mutual labels:  rspec, minitest
With model
Dynamically build an Active Record model (with table) within a test context
Stars: ✭ 119 (+75%)
Mutual labels:  rspec, minitest
Factory trace
Simple tool to maintain factories and traits from FactoryBot
Stars: ✭ 184 (+170.59%)
Mutual labels:  rspec, minitest
bdd
Given/When/Then/And/But output to RSpec and Minitest
Stars: ✭ 33 (-51.47%)
Mutual labels:  rspec, minitest
knapsack pro-ruby
Knapsack Pro gem splits tests across parallel CI nodes and makes sure that tests will run in optimal time on each node.
Stars: ✭ 101 (+48.53%)
Mutual labels:  rspec, minitest
betterminitest.com
Learn how to write better Minitest tests
Stars: ✭ 20 (-70.59%)
Mutual labels:  minitest
yo-ruby
🌈 ✨ A super awesome Ruby wrapper of the Yo API.
Stars: ✭ 15 (-77.94%)
Mutual labels:  rspec
CtCI-with-Ruby-TDD
Cracking the Coding Interview with Ruby and TDD
Stars: ✭ 44 (-35.29%)
Mutual labels:  rspec
minitest-matchers vaccine
💉 Adds matcher support to minitest without all the other RSpec-style expectation "infections."
Stars: ✭ 33 (-51.47%)
Mutual labels:  minitest

factory_bot-preload

ruby-tests Gem Gem

We all love Rails fixtures because they're fast, but we hate to deal with YAML/CSV/SQL files. Here enters factory_bot (FB).

Now, you can easily create records by using predefined factories. The problem is that hitting the database everytime to create records is pretty slow. And believe me, you'll feel the pain when you have lots of tests/specs.

So here enters Factory Bot Preload (FBP). You can define which factories will be preloaded, so you don't have to recreate it every time (that will work for 99.37% of the time, according to statistics I just made up).

Installation

gem install factory_bot-preload

Intructions

Setup

Add both FB and FBP to your Gemfile:

source "https://rubygems.org"

gem "rails"
gem "pg"

group :test, :development do
  gem "factory_bot"
  gem "factory_bot-preload", require: false
end

Notice that adding require: false is important; otherwise you won't be able to run commands such as rails db:test:prepare.

RSpec Setup

On your spec/spec_helper.rb file, make sure that transactional fixtures are enabled. Here's is my file without all those RSpec comments:

ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
require "rspec/rails"

# First, load factory_bot/preload.
require "factory_bot/preload"

# Then load your factories
Dir[Rails.root.join("spec/support/factories/**/*.rb")].each do |file|
  require file
end

RSpec.configure do |config|
  config.use_transactional_fixtures = true
  config.mock_with :rspec
end

You may want to configure the generated helper names. For instance, imagine you have a namespace like MyApp::Models::User. That'd generate a helper method like myapp_models_user. If you don't have conflicting names, you can strip myapp_models_ like this:

FactoryBot::Preload.helper_name = lambda do |class_name, helper_name|
  helper_name.gsub(/^myapp_models_/, "")
end

Minitest Setup

On your test/test_helper.rb file, make sure that transaction fixtures are enabled. Here's what your file may look like:

ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"

module ActiveSupport
  class TestCase
    self.use_instantiated_fixtures = true
  end
end

# First, load factory_bot/preload.
require "factory_bot/preload"

# Then load your factories.
Dir["./test/support/factories/**/*.rb"].each do |file|
  require file
end

# Finally, setup minitest.
# Your factories won't behave correctly unless you
# call `FactoryBot::Preload.minitest` after loading them.
FactoryBot::Preload.minitest

Usage

Create your factories and load it from your setup file (either test/test_helper.rb or spec/spec_helper.rb) You may have something like this:

FactoryBot.define do
  factory :user do
    name "John Doe"
    sequence(:email) {|n| "john#{n}@example.org" }
    sequence(:username) {|n| "john#{n}" }
    password "test"
    password_confirmation "test"
  end

  factory :projects do
    name "My Project"
    association :user
  end
end

To define your preloadable factories, just use the preload method:

FactoryBot.define do
  factory :user do
    name "John Doe"
    sequence(:email) {|n| "john#{n}@example.org" }
    sequence(:username) {|n| "john#{n}" }
    password "test"
    password_confirmation "test"
  end

  factory :projects do
    name "My Project"
    association :user
  end

  preload do
    factory(:john) { create(:user) }
    factory(:myapp) { create(:project, user: users(:john)) }
  end
end

You can also use preloaded factories on factory definitions.

FactoryBot.define do
  factory :user do
    # ...
  end

  factory :projects do
    name "My Project"
    user { users(:john) }
  end

  preload do
    factory(:john) { create(:user) }
    factory(:myapp) { create(:project, user: users(:john)) }
  end
end

Like Rails fixtures, FBP will define methods for each model. You can use it on your examples and alike.

require "test_helper"

class UserTest < ActiveSupport::TestCase
  test "returns john's record" do
    assert_instance_of User, users(:john)
  end

  test "returns myapp's record" do
    assert_equal users(:john), projects(:myapp).user
  end
end

Or if you're using RSpec:

require "spec_helper"

describe User do
  let(:user) { users(:john) }

  it "returns john's record" do
    users(:john).should be_an(User)
  end

  it "returns myapp's record" do
    projects(:myapp).user.should == users(:john)
  end
end

That's it!

Maintainer

Contributors

Contributing

For more details about how to contribute, please read https://github.com/fnando/factory_bot-preload/blob/main/CONTRIBUTING.md.

License

The gem is available as open source under the terms of the MIT License. A copy of the license can be found at https://github.com/fnando/factory_bot-preload/blob/main/LICENSE.md.

Code of Conduct

Everyone interacting in the factory_bot-preload project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].