mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Added the create, build, and attributes class methods on Factory
This commit is contained in:
parent
f6db21769d
commit
218ed9a055
2 changed files with 65 additions and 6 deletions
|
@ -2,7 +2,7 @@ class Factory
|
|||
|
||||
# The list of Factory instances created using define
|
||||
cattr_accessor :factories
|
||||
self.factories = []
|
||||
self.factories = {}
|
||||
|
||||
attr_reader :name
|
||||
|
||||
|
@ -21,7 +21,7 @@ class Factory
|
|||
def self.define (name, options = {})
|
||||
instance = Factory.new(name, options)
|
||||
yield(instance)
|
||||
self.factories << instance
|
||||
self.factories[name] = instance
|
||||
end
|
||||
|
||||
# Calculates the class that should be instantiated by generation methods.
|
||||
|
@ -145,4 +145,26 @@ class Factory
|
|||
instance
|
||||
end
|
||||
|
||||
class << self
|
||||
|
||||
def attributes (name, attrs = {})
|
||||
factory_by_name(name).attributes(attrs)
|
||||
end
|
||||
|
||||
def build (name, attrs = {})
|
||||
factory_by_name(name).build(attrs)
|
||||
end
|
||||
|
||||
def create (name, attrs = {})
|
||||
factory_by_name(name).create(attrs)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def factory_by_name (name)
|
||||
factories[name] or raise ArgumentError.new("No such factory: #{name.inspect}")
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -45,8 +45,9 @@ class FactoryTest < Test::Unit::TestCase
|
|||
|
||||
should "add the factory to the list of factories" do
|
||||
Factory.define(@name) {|f| }
|
||||
assert Factory.factories.include?(@factory),
|
||||
"Factories: #{Factory.factories.inspect}"
|
||||
assert_equal Factory.factories[@name],
|
||||
@factory,
|
||||
"Factories: #{Factory.factories.inspect}"
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -178,19 +179,22 @@ class FactoryTest < Test::Unit::TestCase
|
|||
|
||||
should_instantiate_class
|
||||
|
||||
should "not save the instance" do
|
||||
assert @instance.new_record?
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "when creating an instance" do
|
||||
|
||||
setup do
|
||||
User.delete_all
|
||||
@instance = @factory.create
|
||||
end
|
||||
|
||||
should_instantiate_class
|
||||
|
||||
should "save the instance" do
|
||||
assert_equal 1, @class.count
|
||||
assert !@instance.new_record?
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -205,4 +209,37 @@ class FactoryTest < Test::Unit::TestCase
|
|||
|
||||
end
|
||||
|
||||
context "Factory class method" do
|
||||
|
||||
setup do
|
||||
@name = :user
|
||||
@attrs = { :last_name => 'Override' }
|
||||
@first_name = 'Johnny'
|
||||
@last_name = 'Winter'
|
||||
@class = User
|
||||
|
||||
Factory.define(@name) do |u|
|
||||
u.first_name @first_name
|
||||
u.last_name { @last_name }
|
||||
u.email 'jwinter@guitar.org'
|
||||
end
|
||||
|
||||
@factory = Factory.factories[@name]
|
||||
end
|
||||
|
||||
[:build, :create, :attributes].each do |method|
|
||||
|
||||
should "delegate the #{method} method to the factory instance" do
|
||||
@factory.expects(method).with(@attrs)
|
||||
Factory.send(method, @name, @attrs)
|
||||
end
|
||||
|
||||
should "raise an ArgumentError when #{method} is called with a nonexistant factory" do
|
||||
assert_raise(ArgumentError) { Factory.send(method, :bogus) }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue