Skip to content

muriloime/to_factory

Repository files navigation

ToFactory 🔧

Build StatusDependency StatusCode ClimateTest CoverageGem VersionLicenseBadges

Easily add factories with valid data for an existing project.

If you find yourself retro-fitting tests this gem will save you some of the legwork.

  • auto-generate all factories
  • adhoc generate from existing records
  • unintrusively update factory files in place
  • display factory definition for a record
  • parse and write FactoryBot syntax or older Factory.define syntax

Tested against Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.x

Warning ⚠️

ToFactory writes into the spec/factories folder. Whilst it is tested and avoids overwriting existing factories, it is recommended that you execute after committing or when in a known safe state.

Installation 📁

#Gemfile#add to whichever environments you want to generate data fromgroup:test,:developmentdogem'to_factory'end

For Ruby < 2.x and older FactoryBot syntax etc

group:test,:developmentdogem"to_factory","~> 0.2.1"end
git add spec/factories git commit -m "I know what I am doing" rails c >ToFactory()

Example 💻

#Generate all factoriesToFactory()#outputs the first record of each ActiveRecord::Base subclass in the models folder#to spec/factories#Choose input/output directoriesToFactory.models="models/this/subfolder/only"#default "./app/models"ToFactory.factories="spec/support/factories"#default "./spec/factories"ToFactory()#Exclude classesToFactory(exclude: [User,Project])#Use Adhoc instances from the consoleToFactoryUser.last#writes to spec/factories/user.rbFactoryBot.definefactory(:user)do |u| email"[email protected]"name"Mike"endend#List defined factory namesToFactory.definitions#=> [:user, :admin, :project]#Display definition from recordToFactory.definition_for@user#Display existing definition from nameToFactory.definition_for:admin#doesn't overwrite existing factoriesToFactoryUser.last#Exception =>#ToFactory::AlreadyExists: an item for each of the following keys :user already exists#Choose specific nameToFactory:admin=>User.last#appends to spec/factories/user.rb

Other useful projects

If you are adding specs to an existing project you may want to look at:

About

Auto generate factories from data

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby100.0%