Creating FactoryGirl for ActiveRecord classes is quite handy. Here we will learn creating FactoryGirl for NonActiveRecord classes.

Assume you have a class Note in ‘app/models’ directory

class Note
  attr_accessor :text, :admin_id, :time
end

Create Factory in ‘spec/factories’ directory

Factory.define :note do |f|
    f.text                "Lorem Ipsum"
    f.admin_id            1
    f.time                Time.now
end

We can use this factory in our specs like:

note = Factory.build(:note)

Note:

Methods like create, save other than new would not work.