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

Created a basic Factory class and implemented Factory.define

This commit is contained in:
Joe Ferris 2008-05-28 18:54:54 -04:00
parent 6f663a7740
commit b01006cf37
2 changed files with 65 additions and 2 deletions

View file

@ -1,3 +1,28 @@
class Factory
# The list of Factory instances created using define
cattr_accessor :factories
self.factories = []
attr_reader :name
# Defines a new factory that can be used by the generation methods (create,
# build, and stub) to build new objects.
#
# Arguments:
# name: (Symbol)
# A unique name used to identify this factory.
#
# Yields:
# The newly created factory (Factory)
def self.define (name)
instance = Factory.new(name)
yield(instance)
self.factories << instance
end
def initialize (name) #:nodoc:
@name = name
end
end

View file

@ -2,8 +2,46 @@ require(File.join(File.dirname(__FILE__), 'test_helper'))
class FactoryTest < Test::Unit::TestCase
def test_truth
assert true
context "defining a factory" do
setup do
@name = :user
@factory = mock('factory')
Factory.stubs(:new).returns(@factory)
end
should "create a new factory" do
Factory.expects(:new).with(@name)
Factory.define(@name) {|f| }
end
should "pass the factory do the block" do
yielded = nil
Factory.define(@name) do |y|
yielded = y
end
assert_equal @factory, yielded
end
should "add the factory to the list of factories" do
Factory.define(@name) {|f| }
assert Factory.factories.include?(@factory),
"Factories: #{Factory.factories.inspect}"
end
end
context "a factory" do
setup do
@name = :user
@factory = Factory.new(@name)
end
should "have a name" do
assert_equal @name, @factory.name
end
end
end