Created
June 19, 2012 22:20
-
-
Save eric-smartlove/2956851 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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