Skip to content

Instantly share code, notes, and snippets.

@md5
Last active November 13, 2015 20:32
Show Gist options
  • Save md5/00a4416c3f229695a3f3 to your computer and use it in GitHub Desktop.
Save md5/00a4416c3f229695a3f3 to your computer and use it in GitHub Desktop.
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
if ENV['RAILS_VERSION']
gem 'rails', ENV['RAILS_VERSION']
else
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'sprockets', github: 'rails/sprockets'
gem 'sprockets-rails', github: 'rails/sprockets-rails'
gem 'sass-rails', github: 'rails/sass-rails'
end
gem 'sqlite3'
gem 'factory_girl'
end
require 'active_record'
require 'factory_girl'
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 :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post, required: true
end
FactoryGirl.define do
factory :post do
trait :with_comment do
after(:create) do |post|
create_list(:comment, 2, post: post)
end
end
end
factory :comment do
end
end
class BugTest < Minitest::Test
def test_association_stuff
post = FactoryGirl.create(:post, :with_comment)
assert_equal 2, post.comments.count
assert_equal 2, Comment.count
assert_equal post.id, Comment.first.post.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment