diff --git a/factory_girl.gemspec b/factory_girl.gemspec index 9504f8c..186ae7e 100644 --- a/factory_girl.gemspec +++ b/factory_girl.gemspec @@ -1,5 +1,5 @@ $LOAD_PATH << File.join(File.dirname(__FILE__), 'lib') -require 'factory_girl' +require 'factory_girl/version' Gem::Specification.new do |s| s.name = %q{factory_girl} s.version = FactoryGirl::VERSION diff --git a/lib/factory_girl.rb b/lib/factory_girl.rb index 6483c47..af84950 100644 --- a/lib/factory_girl.rb +++ b/lib/factory_girl.rb @@ -16,10 +16,7 @@ require 'factory_girl/syntax/default' require 'factory_girl/syntax/vintage' require 'factory_girl/find_definitions' require 'factory_girl/deprecated' - -module FactoryGirl - VERSION = "1.3.1" -end +require 'factory_girl/version' if defined?(Rails) && Rails::VERSION::MAJOR == 2 require 'factory_girl/rails2' diff --git a/lib/factory_girl/factory.rb b/lib/factory_girl/factory.rb index 2c16ca3..42ae34a 100644 --- a/lib/factory_girl/factory.rb +++ b/lib/factory_girl/factory.rb @@ -10,7 +10,7 @@ module FactoryGirl end def self.register_factory(factory, options = {}) - name = options[:as] || factory.factory_name + name = options[:as] || factory.name if self.factories[name] raise DuplicateDefinitionError, "Factory already defined: #{name}" end @@ -30,11 +30,16 @@ module FactoryGirl end class Factory - attr_reader :factory_name #:nodoc: + attr_reader :name #:nodoc: attr_reader :attributes #:nodoc: + def factory_name + puts "WARNING: factory.factory_name is deprecated. Use factory.name instead." + name + end + def class_name #:nodoc: - @options[:class] || factory_name + @options[:class] || name end def build_class #:nodoc: @@ -45,11 +50,11 @@ module FactoryGirl @options[:default_strategy] || :create end - def initialize (name, options = {}) #:nodoc: + def initialize(name, options = {}) #:nodoc: assert_valid_options(options) - @factory_name = factory_name_for(name) - @options = options - @attributes = [] + @name = factory_name_for(name) + @options = options + @attributes = [] end def inherit_from(parent) #:nodoc: @@ -71,8 +76,8 @@ module FactoryGirl if attribute_defined?(name) raise AttributeDefinitionError, "Attribute already defined: #{name}" end - if attribute.respond_to?(:factory) && attribute.factory == self.factory_name - raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.factory_name}'" + if attribute.respond_to?(:factory) && attribute.factory == self.name + raise AssociationDefinitionError, "Self-referencing association '#{name}' in factory '#{self.name}'" end @attributes << attribute end @@ -98,11 +103,7 @@ module FactoryGirl end def human_name(*args, &block) - if args.size == 0 && block.nil? - factory_name.to_s.gsub('_', ' ') - else - add_attribute(:human_name, *args, &block) - end + name.to_s.gsub('_', ' ') end def associations @@ -122,7 +123,7 @@ module FactoryGirl end end - def factory_name_for (class_or_to_s) + def factory_name_for(class_or_to_s) if class_or_to_s.respond_to?(:to_sym) class_or_to_s.to_sym else diff --git a/lib/factory_girl/step_definitions.rb b/lib/factory_girl/step_definitions.rb index b526a41..e23c48c 100644 --- a/lib/factory_girl/step_definitions.rb +++ b/lib/factory_girl/step_definitions.rb @@ -31,22 +31,22 @@ FactoryGirl.factories.values.each do |factory| end Given /^an? #{factory.human_name} exists$/ do - Factory(factory.factory_name) + Factory(factory.name) end Given /^(\d+) #{factory.human_name.pluralize} exist$/ do |count| - count.to_i.times { Factory(factory.factory_name) } + count.to_i.times { Factory(factory.name) } end if factory.build_class.respond_to?(:columns) factory.build_class.columns.each do |column| human_column_name = column.name.downcase.gsub('_', ' ') Given /^an? #{factory.human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value| - Factory(factory.factory_name, column.name => value) + Factory(factory.name, column.name => value) end Given /^(\d+) #{factory.human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value| - count.to_i.times { Factory(factory.factory_name, column.name => value) } + count.to_i.times { Factory(factory.name, column.name => value) } end end end diff --git a/lib/factory_girl/version.rb b/lib/factory_girl/version.rb new file mode 100644 index 0000000..07e72b7 --- /dev/null +++ b/lib/factory_girl/version.rb @@ -0,0 +1,4 @@ +module FactoryGirl + VERSION = "2.0.0.beta1" +end + diff --git a/spec/acceptance/syntax/vintage_spec.rb b/spec/acceptance/syntax/vintage_spec.rb index 4a3224c..39e2135 100644 --- a/spec/acceptance/syntax/vintage_spec.rb +++ b/spec/acceptance/syntax/vintage_spec.rb @@ -52,7 +52,7 @@ describe "defining a factory" do @name = :user @factory = "factory" @proxy = "proxy" - stub(@factory).factory_name { @name } + stub(@factory).name { @name } @options = { :class => 'magic' } stub(FactoryGirl::Factory).new { @factory } stub(FactoryGirl::DefinitionProxy).new { @proxy } diff --git a/spec/factory_girl/factory_spec.rb b/spec/factory_girl/factory_spec.rb index bf7a77c..8561beb 100644 --- a/spec/factory_girl/factory_spec.rb +++ b/spec/factory_girl/factory_spec.rb @@ -4,7 +4,7 @@ describe FactoryGirl::Factory, "registering a factory" do before do @name = :user @factory = "factory" - stub(@factory).factory_name { @name } + stub(@factory).name { @name } end it "should add the factory to the list of factories" do @@ -29,6 +29,10 @@ describe FactoryGirl::Factory do end it "should have a factory name" do + @factory.name.should == @name + end + + it "responds to factory_name" do @factory.factory_name.should == @name end @@ -238,7 +242,7 @@ describe FactoryGirl::Factory, "when defined with a class instead of a name" do end it "should guess the name from the class" do - @factory.factory_name.should == @name + @factory.name.should == @name end it "should use the class as the build class" do @@ -268,7 +272,7 @@ describe FactoryGirl::Factory, "with a name ending in s" do end it "should have a factory name" do - @factory.factory_name.should == @name + @factory.name.should == @name end it "should have a build class" do @@ -283,7 +287,7 @@ describe FactoryGirl::Factory, "with a string for a name" do end it "should convert the string to a symbol" do - @factory.factory_name.should == @name + @factory.name.should == @name end end @@ -380,3 +384,11 @@ describe FactoryGirl::Factory do end +describe FactoryGirl::Factory, "with an underscore in the name" do + subject { FactoryGirl::Factory.new("happy_users") } + + it "has a human name" do + subject.human_name.should == 'happy users' + end +end +