Pablo Pablo - 4 months ago 14
Ruby Question

Why is factory girl picking up the wrong class?

I have a factory girl factory defined as follows:

FactoryGirl.define do
factory :task do

factory :facebook_task, class: Task::Facebook do
# ...
end

factory :twitter_task, class: Task::Twitter do
# ...
end
end
end


for models defined as follows:

class Task < ActiveRecord::Base
end

class Task::Twitter < Task
end

class Task::Facebook < Task
end


It all work fine until I add the twitter gem which clearly defines a ::Twitter module

.../test/factories/tasks.rb:12: warning: toplevel constant Twitter referenced by Task::Twitter
Running via Spring preloader in process 51820
rake aborted!
NoMethodError: undefined method `new' for Twitter:Module
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/new_constructor.rb:9:in `new'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
Cleaning database...done.
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/invocation_tracker.rb:11:in `method_missing'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/configuration.rb:19:in `block in initialize'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `instance_exec'
Creating Beatles.../Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `build_class_instance'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:13:in `object'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluation.rb:12:in `object'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/create.rb:9:in `result'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory.rb:42:in `run'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/notifications.rb:166:in `instrument'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:28:in `run'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
/Users/pupeno/Documents/code/buzz/lib/tasks/sample_data.rake:25:in `block (2 levels) in <top (required)>'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/pupeno/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'
Tasks: TOP => sample_data:generate
(See full trace by running task with --trace)


Why is Factory Girl trying to instantiate Twitter instead of Task::Twitter?

tasks.rb:12 is this line:

factory :twitter_task, class: Task::Twitter do


I don't see how it's referencing the toplevel constant Twitter.

Answer

try to use class name as a string:

factory :facebook_task, class: 'Task::Facebook' do
  # ...
end

instead

factory :facebook_task, class: Task::Facebook do
  # ...
end