1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/lib/factory_girl/proxy/stub.rb

62 lines
1.4 KiB
Ruby
Raw Normal View History

module FactoryGirl
class Proxy
2009-02-17 16:38:15 -05:00
class Stub < Proxy #:nodoc:
@@next_id = 1000
def initialize(klass, callbacks = [])
super
result_instance.id = next_id
result_instance.instance_eval do
def persisted?
!new_record?
end
def created_at
@created_at ||= Time.now
end
def new_record?
id.nil?
end
def save(*args)
raise "stubbed models are not allowed to access the database"
end
def destroy(*args)
raise "stubbed models are not allowed to access the database"
end
def connection
raise "stubbed models are not allowed to access the database"
end
def reload
raise "stubbed models are not allowed to access the database"
end
def update_attribute(*args)
raise "stubbed models are not allowed to access the database"
end
end
end
def association(factory_name, overrides = {})
factory = FactoryGirl.factory_by_name(factory_name)
2011-10-20 15:33:37 -04:00
factory.run(Proxy::Stub, overrides.except(:method))
end
def result(to_create)
2009-10-09 23:46:19 -05:00
run_callbacks(:after_stub)
result_instance
end
2011-11-23 13:47:24 -05:00
private
def next_id
@@next_id += 1
end
end
end
end