Skip to content

Instantly share code, notes, and snippets.

@thdaraujo
Created July 18, 2024 01:22
Show Gist options
  • Save thdaraujo/c48697fcef5c217acccd24a1986ab982 to your computer and use it in GitHub Desktop.
Save thdaraujo/c48697fcef5c217acccd24a1986ab982 to your computer and use it in GitHub Desktop.
Test Faker against Rails and FactoryBot
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '3.2.4'
gem 'rails', '7.1.3.4'
gem 'factory_bot', '6.4.6'
gem 'faker', '3.4.2'
gem "sqlite3", "~> 1.4"
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.string :email
end
end
class User < ActiveRecord::Base
end
FactoryBot.define do
factory :user do
email { Faker::Internet.email }
end
end
class BugTest < Minitest::Test
def test_email
10_000.times do
puts FactoryBot.build(:user).email
assert FactoryBot.build(:user).email.present?
assert FactoryBot.create(:user).email.present?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment