mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
#6 - both strings and symbols can be used as factory names
This commit is contained in:
parent
f3c6f6562e
commit
e582435d34
3 changed files with 39 additions and 6 deletions
|
@ -22,7 +22,7 @@ class Factory
|
|||
def self.define (name, options = {})
|
||||
instance = Factory.new(name, options)
|
||||
yield(instance)
|
||||
self.factories[name] = instance
|
||||
self.factories[instance.factory_name] = instance
|
||||
end
|
||||
|
||||
# Defines a new sequence that can be used to generate unique values in a specific format.
|
||||
|
@ -66,7 +66,7 @@ class Factory
|
|||
|
||||
def initialize (name, options = {}) #:nodoc:
|
||||
options.assert_valid_keys(:class)
|
||||
@factory_name = name
|
||||
@factory_name = name.to_sym
|
||||
@options = options
|
||||
|
||||
@static_attributes = {}
|
||||
|
@ -188,7 +188,7 @@ class Factory
|
|||
private
|
||||
|
||||
def factory_by_name (name)
|
||||
factories[name] or raise ArgumentError.new("No such factory: #{name.inspect}")
|
||||
factories[name.to_sym] or raise ArgumentError.new("No such factory: #{name.to_s}")
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -26,12 +26,13 @@ class FactoryTest < Test::Unit::TestCase
|
|||
setup do
|
||||
@name = :user
|
||||
@factory = mock('factory')
|
||||
@factory.stubs(:factory_name).returns(@name)
|
||||
@options = { :class => 'magic' }
|
||||
Factory.stubs(:new).returns(@factory)
|
||||
end
|
||||
|
||||
should "create a new factory using the specified name and options" do
|
||||
Factory.expects(:new).with(@name, @options)
|
||||
Factory.expects(:new).with(@name, @options).returns(@factory)
|
||||
Factory.define(@name, @options) {|f| }
|
||||
end
|
||||
|
||||
|
@ -267,6 +268,33 @@ class FactoryTest < Test::Unit::TestCase
|
|||
|
||||
end
|
||||
|
||||
context "a factory with a string for a name" do
|
||||
|
||||
setup do
|
||||
@name = :user
|
||||
@factory = Factory.new(@name.to_s) {}
|
||||
end
|
||||
|
||||
should "convert the string to a symbol" do
|
||||
assert_equal @name, @factory.factory_name
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "a factory defined with a string name" do
|
||||
|
||||
setup do
|
||||
Factory.factories = {}
|
||||
@name = :user
|
||||
@factory = Factory.define(@name.to_s) {}
|
||||
end
|
||||
|
||||
should "store the factory using a symbol" do
|
||||
assert_equal @factory, Factory.factories[@name]
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "Factory class" do
|
||||
|
||||
setup do
|
||||
|
@ -296,6 +324,11 @@ class FactoryTest < Test::Unit::TestCase
|
|||
assert_raise(ArgumentError) { Factory.send(method, :bogus) }
|
||||
end
|
||||
|
||||
should "recognize either 'name' or :name for Factory.#{method}" do
|
||||
assert_nothing_raised { Factory.send(method, @name.to_s) }
|
||||
assert_nothing_raised { Factory.send(method, @name.to_sym) }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
should "call the create method from the top-level Factory() method" do
|
||||
|
|
|
@ -10,7 +10,7 @@ class IntegrationTest < Test::Unit::TestCase
|
|||
f.email {|a| "#{a.first_name}.#{a.last_name}@example.com".downcase }
|
||||
end
|
||||
|
||||
Factory.define :post do |f|
|
||||
Factory.define 'post' do |f|
|
||||
f.name 'Test Post'
|
||||
f.author {|a| a.association(:user) }
|
||||
end
|
||||
|
@ -79,7 +79,7 @@ class IntegrationTest < Test::Unit::TestCase
|
|||
context "a created instance" do
|
||||
|
||||
setup do
|
||||
@instance = Factory.create(:post)
|
||||
@instance = Factory.create('post')
|
||||
end
|
||||
|
||||
should "be saved" do
|
||||
|
|
Loading…
Reference in a new issue