Deprecate the vintage syntax

This commit is contained in:
Joshua Clayton 2012-03-09 17:45:42 -05:00
parent 6c29b11477
commit 6fe00e76c1
8 changed files with 26 additions and 14 deletions

View File

@ -8,7 +8,7 @@ module FactoryGirl
# Raised when a factory is defined with the same name as a previously-defined factory.
class DuplicateDefinitionError < RuntimeError; end
# Raised when calling Factory.sequence from a dynamic attribute block
# Raised when attempting to register a sequence from a dynamic attribute block
class SequenceAbuseError < RuntimeError; end
# Raised when defining an invalid attribute:

View File

@ -21,6 +21,7 @@ module FactoryGirl
# Yields: +Factory+
# The newly created factory.
def self.define(name, options = {})
$stderr.puts "DEPRECATION WARNING: Factory.define is deprecated; use the FactoryGirl.define block syntax to declare your factory."
factory = FactoryGirl::Factory.new(name, options)
proxy = FactoryGirl::DefinitionProxy.new(factory)
yield(proxy)
@ -43,6 +44,7 @@ module FactoryGirl
#
# Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
def self.sequence(name, start_value = 1, &block)
$stderr.puts "DEPRECATION WARNING: Factory.sequence is deprecated; use the FactoryGirl.define block syntax to declare your sequence."
FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
end
@ -55,6 +57,7 @@ module FactoryGirl
# Returns:
# The next value in the sequence. (Object)
def self.next(name)
$stderr.puts "DEPRECATION WARNING: Factory.next is deprecated; use FactoryGirl.generate instead."
FactoryGirl.generate(name)
end
@ -83,26 +86,31 @@ module FactoryGirl
# # will be used instead.
# Factory(:post, user_id: 1)
def self.alias(pattern, replace)
$stderr.puts "DEPRECATION WARNING: Factory.alias is deprecated; use FactoryGirl.aliases << [pattern, replace] instead."
FactoryGirl.aliases << [pattern, replace]
end
# Alias for FactoryGirl.attributes_for
def self.attributes_for(name, overrides = {})
$stderr.puts "DEPRECATION WARNING: Factory.attributes_for is deprecated; use FactoryGirl.attributes_for instead."
FactoryGirl.attributes_for(name, overrides)
end
# Alias for FactoryGirl.build
def self.build(name, overrides = {})
$stderr.puts "DEPRECATION WARNING: Factory.build is deprecated; use FactoryGirl.build instead."
FactoryGirl.build(name, overrides)
end
# Alias for FactoryGirl.create
def self.create(name, overrides = {})
$stderr.puts "DEPRECATION WARNING: Factory.create is deprecated; use FactoryGirl.create instead."
FactoryGirl.create(name, overrides)
end
# Alias for FactoryGirl.build_stubbed.
def self.stub(name, overrides = {})
$stderr.puts "DEPRECATION WARNING: Factory.stub is deprecated; use FactoryGirl.build_stubbed instead."
FactoryGirl.build_stubbed(name, overrides)
end
end
@ -112,7 +120,8 @@ module FactoryGirl
# Example:
# Factory(:user, name: 'Joe')
def Factory(name, attrs = {})
::Factory.create(name, attrs)
$stderr.puts "DEPRECATION WARNING: Factory(:name) is deprecated; use FactoryGirl.create(:name) instead."
FactoryGirl.create(name, attrs)
end
end
end

View File

@ -2,7 +2,7 @@ require "spec_helper"
describe "aliases and overrides" do
before do
Factory.alias /one/, "two"
FactoryGirl.aliases << [/one/, "two"]
define_model("User", two: :string, one: :string)

View File

@ -29,13 +29,13 @@ describe "modifying inherited factories with traits" do
end
it "returns the correct value for overridden attributes from traits" do
Factory.build(:male_user).gender.should == "Male"
FactoryGirl.build(:male_user).gender.should == "Male"
end
it "returns the correct value for overridden attributes from traits defining multiple attributes" do
Factory.build(:female_user).gender.should == "Female"
Factory.build(:female_user).age.should == 25
Factory.build(:female_user).admin.should == true
FactoryGirl.build(:female_user).gender.should == "Female"
FactoryGirl.build(:female_user).age.should == 25
FactoryGirl.build(:female_user).admin.should == true
end
it "allows modification of attributes created via traits" do
@ -45,8 +45,8 @@ describe "modifying inherited factories with traits" do
end
end
Factory.build(:male_user).gender.should == "Male"
Factory.build(:male_user).age.should == 20
Factory.build(:male_user).admin.should == true
FactoryGirl.build(:male_user).gender.should == "Male"
FactoryGirl.build(:male_user).age.should == 20
FactoryGirl.build(:male_user).admin.should == true
end
end

View File

@ -6,7 +6,10 @@ describe "a blueprint" do
before do
define_model('User', first_name: :string, last_name: :string, email: :string)
Factory.sequence(:email) { |n| "somebody#{n}@example.com" }
FactoryGirl.define do
sequence(:email) { |n| "somebody#{n}@example.com" }
end
User.blueprint do
first_name { 'Bill' }
last_name { 'Nye' }

View File

@ -21,7 +21,7 @@ end
describe Factory, "after defining an alias" do
before do
Factory.alias(/(.*)_suffix/, '\1')
FactoryGirl.aliases << [/(.*)_suffix/, '\1']
end
subject { FactoryGirl.aliases_for(:test_suffix) }

View File

@ -38,7 +38,7 @@ describe FactoryGirl::Attribute::Dynamic do
end
context "with a block returning a sequence" do
let(:block) { lambda { Factory.sequence(:email) } }
let(:block) { lambda { FactoryGirl.register_sequence(FactoryGirl::Sequence.new(:email, 1) {|n| "foo#{n}" }) } }
it "raises a sequence abuse error" do
expect { subject.to_proc.call }.to raise_error(FactoryGirl::SequenceAbuseError)

View File

@ -86,7 +86,7 @@ describe FactoryGirl::Factory do
describe "overriding an attribute with an alias" do
before do
@factory.declare_attribute(FactoryGirl::Declaration::Static.new(:test, 'original'))
Factory.alias(/(.*)_alias/, '\1')
FactoryGirl.aliases << [/(.*)_alias/, '\1']
@result = @factory.run(FactoryGirl::Strategy::AttributesFor,
test_alias: 'new')
end