Defining factories for a class inside a module
Asked Answered
R

1

22

I am pretty new to factory_girl and I have the following problem.

I have a class say of the form:

class Fruit::Apple < ActiveRecord::Base
  ...
end

Suppose I need to create factories for this class. How should I do it? In other words is there a set_fixture_class equivalent for factories?

My apple_factory.rb is as follows:

FactoryGirl.define do |f|
  factory 'apple' do
    variety : washington
  end
end

I have all the required gems and my folder structure is of the form test/factory/apple_factory.rb. When I execute my tests I get uninitialized constant Apple. Where do I set the class definition? Do I need to set a factory helper?

Rayon answered 27/11, 2012 at 18:20 Comment(0)
G
39

Looks like you are using namespaces for your models.

Take a look at this question and answer: Using factory_girl_rails with Rspec on namespaced models

Factory.define :apple, :class => Fruit::Apple do |f|
  f.variety 'Fuji'
end
God answered 27/11, 2012 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.