Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing simple STI with FactoryGirl

I have got a class, that is the base of some other classes that specializes the behavior:

class Task < ActiveRecord::Base
  attr_accessible :type, :name, :command
  validates_presence_of :type, :name, :command

  # some methods I would like to test

end

The class CounterTask inherits from Task

class CounterTask < Task 
end

This all works fine until I am trying to test the base class, since it must have a type.

FactoryGirl.define do
  factory :task do
    sequence(:name) { |n| "name_#{n}" }
    sequence(:command) { |n|  "command_#{n}" }
  end
end

How would you test the basic functionality of the superclass?

like image 502
Mark Avatar asked Aug 20 '13 09:08

Mark


1 Answers

You can declare the definitions of factories as follow:

FactoryGirl.define do
  factory :task, class: 'Task' do
    shop
    currency
    value 10
  end

  factory :counter_task, parent: :task, class: 'CounterTask' do
  end
end

And now you can test base class isolated from its own factory and the same for each inherited class.

like image 163
emaxi Avatar answered Nov 20 '22 20:11

emaxi