Skip to content

Commit

Permalink
Check for undefined constants/methods on Factory on FactoryGirl to pr…
Browse files Browse the repository at this point in the history
…eserve backwards compatibility
  • Loading branch information
jferris committed Jul 7, 2010
1 parent 8b4a6a1 commit 4824125
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/factory_girl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'factory_girl/definition_proxy'
require 'factory_girl/syntax/default'
require 'factory_girl/find_definitions'
require 'factory_girl/deprecated'

module FactoryGirl
VERSION = "1.3.1"
Expand Down
18 changes: 18 additions & 0 deletions lib/factory_girl/deprecated.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Factory
def self.method_missing(name, *args, &block)
if FactoryGirl.respond_to?(name)
$stderr.puts "DEPRECATION WARNING: Change Factory.#{name} to FactoryGirl.#{name}"
FactoryGirl.send(name, *args, &block)
else
super(name, *args, &block)
end
end

def self.const_missing(name)
if FactoryGirl.const_defined?(name)
FactoryGirl.const_get(name)
else
super(name)
end
end
end
66 changes: 66 additions & 0 deletions spec/factory_girl/deprecated_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'spec_helper'

describe "accessing an undefined method on Factory that is defined on FactoryGirl" do
let(:method_name) { :aliases }
let(:return_value) { 'value' }
let(:args) { [1, 2, 3] }

before do
stub($stderr).puts
stub(FactoryGirl, method_name).returns { return_value }

@result = Factory.send(method_name, *args)
end

it "prints a deprecation warning" do
$stderr.should have_received.puts(anything)
end

it "invokes that method on FactoryGirl" do
FactoryGirl.should have_received.method_missing(method_name, *args)
end

it "returns the value from the method on FactoryGirl" do
@result.should == return_value
end
end

describe "accessing an undefined method on Factory that is not defined on FactoryGirl" do
let(:method_name) { :magic_beans }

before do
stub($stderr).puts { raise "Don't print a deprecation warning" }

begin
Factory.send(method_name)
rescue Exception => @raised
end
end

it "raises a NoMethodError" do
@raised.should be_a(NoMethodError)
end
end

describe "accessing an undefined constant on Factory that is defined on FactoryGirl" do
before do
@result = Factory::VERSION
end

it "returns that constant on FactoryGirl" do
@result.should == FactoryGirl::VERSION
end
end

describe "accessing an undefined constant on Factory that is undefined on FactoryGirl" do
it "raises a NameError for Factory" do
begin
Factory::BOGUS
rescue Exception => exception
end

exception.should be_a(NameError)
exception.message.should include("Factory::BOGUS")
end
end

0 comments on commit 4824125

Please sign in to comment.