mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Handle namespaced classes passed as string.
This commit is contained in:
parent
16feec01f1
commit
ecc6e5493e
3 changed files with 27 additions and 1 deletions
|
@ -345,7 +345,10 @@ class Factory
|
|||
|
||||
def class_for (class_or_to_s)
|
||||
if class_or_to_s.respond_to?(:to_sym)
|
||||
Object.const_get(variable_name_to_class_name(class_or_to_s))
|
||||
class_name = variable_name_to_class_name(class_or_to_s)
|
||||
class_name.split('::').inject(Object) do |object, string|
|
||||
object.const_get(string)
|
||||
end
|
||||
else
|
||||
class_or_to_s
|
||||
end
|
||||
|
|
|
@ -398,6 +398,24 @@ describe Factory do
|
|||
end
|
||||
end
|
||||
|
||||
describe "a factory for namespaced class" do
|
||||
before do
|
||||
Factory.factories = {}
|
||||
@name = :settings
|
||||
@class = Admin::Settings
|
||||
end
|
||||
|
||||
it "should build namespaced class passed by string" do
|
||||
factory = Factory.define(@name.to_s, :class => @class.name) {}
|
||||
factory.build_class.should == @class
|
||||
end
|
||||
|
||||
it "should build Admin::Settings class from Admin::Settings string" do
|
||||
factory = Factory.define(@name.to_s, :class => @class.name.underscore) {}
|
||||
factory.build_class.should == @class
|
||||
end
|
||||
end
|
||||
|
||||
describe "after defining a factory" do
|
||||
before do
|
||||
@name = :user
|
||||
|
|
|
@ -41,3 +41,8 @@ class Post < ActiveRecord::Base
|
|||
validates_presence_of :name, :author_id
|
||||
belongs_to :author, :class_name => 'User'
|
||||
end
|
||||
|
||||
module Admin
|
||||
class Settings
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue