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.

Revisions

  1. md5 revised this gist Nov 13, 2015. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions factory_girl_belongs_to_required.rb
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ class Comment < ActiveRecord::Base
    factory :post do
    trait :with_comment do
    after(:create) do |post|
    create(:comment, post: post)
    create_list(:comment, 2, post: post)
    end
    end
    end
    @@ -64,8 +64,8 @@ class BugTest < Minitest::Test
    def test_association_stuff
    post = FactoryGirl.create(:post, :with_comment)

    assert_equal 1, post.comments.count
    assert_equal 1, Comment.count
    assert_equal 2, post.comments.count
    assert_equal 2, Comment.count
    assert_equal post.id, Comment.first.post.id
    end
    end
    end
  2. md5 revised this gist Nov 13, 2015. 1 changed file with 10 additions and 6 deletions.
    16 changes: 10 additions & 6 deletions factory_girl_belongs_to_required.rb
    Original file line number Diff line number Diff line change
    @@ -7,12 +7,16 @@

    gemfile(true) do
    source 'https://rubygems.org'
    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'
    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
  3. md5 created this gist Nov 13, 2015.
    67 changes: 67 additions & 0 deletions factory_girl_belongs_to_required.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    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'
    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'
    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(:comment, 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 1, post.comments.count
    assert_equal 1, Comment.count
    assert_equal post.id, Comment.first.post.id
    end
    end