Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining factories for a class inside a module

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?

like image 765
divroxalwz Avatar asked Nov 27 '12 18:11

divroxalwz


People also ask

Can we define a class inside a class in Ruby?

So, in ruby privates classes can be defined inside a class as a sub-class and declaring them into privates constants, here this private class can be only accessed through the outer-class.

CAN modules have classes in Ruby?

A Ruby module is nothing more than a grouping of objects under a single name. The objects may be constants, methods, classes, or other modules.


1 Answers

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
like image 71
John Naegle Avatar answered Oct 19 '22 05:10

John Naegle