Skip to content

Instantly share code, notes, and snippets.

@eric-smartlove
Created June 19, 2012 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric-smartlove/2956851 to your computer and use it in GitHub Desktop.
Save eric-smartlove/2956851 to your computer and use it in GitHub Desktop.
require "test_helper"
class FactoryGirlTest < ActiveSupport::TestCase
class User < ActiveRecord::Base
end
@@count = 0
def self.count=(value); @@count = value; end
def self.count; @@count; end
FactoryGirl.define do
factory :test_user, class: FactoryGirlTest::User do
factory :test_user_with_callback do
after(:create) do |test_user, _|
FactoryGirlTest.count += 1
end
end
trait :trait_with_callback do
after(:create) do |test_user, _|
FactoryGirlTest.count += 1
end
end
factory :test_user_with_trait_with_callback do
trait_with_callback
end
end
end
test "Callback is called once when defined in a subfactory" do
assert_difference "FactoryGirlTest.count", 1 do
create(:test_user_with_callback)
end
end
test "Callback is called once when defined in a trait" do
assert_difference "FactoryGirlTest.count", 1 do
create(:test_user, :trait_with_callback)
end
end
test "Callback is called once when defined in a trait used in a subfactory" do
assert_difference "FactoryGirlTest.count", 1 do
create(:test_user_with_trait_with_callback)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment