diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index 746066e..ba66b36 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -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 diff --git a/test/factory_test.rb b/test/factory_test.rb index 48cba1f..08dac32 100644 --- a/test/factory_test.rb +++ b/test/factory_test.rb @@ -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 diff --git a/test/integration_test.rb b/test/integration_test.rb index 245ff9e..d1326c0 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -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