Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/markburns/to_factory
Auto generate factories from data
https://github.com/markburns/to_factory
Last synced: 2 months ago
JSON representation
Auto generate factories from data
- Host: GitHub
- URL: https://github.com/markburns/to_factory
- Owner: markburns
- Created: 2011-08-20T08:43:34.000Z (over 13 years ago)
- Default Branch: main
- Last Pushed: 2022-12-31T09:19:20.000Z (about 2 years ago)
- Last Synced: 2024-08-01T13:27:12.657Z (5 months ago)
- Language: Ruby
- Homepage:
- Size: 263 KB
- Stars: 113
- Watchers: 4
- Forks: 16
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
- awesome-ruby-toolbox - to_factory - Autogenerate and append/create factory_girl definitions from the console (Testing / Rails Fixture Replacement)
README
ToFactory :wrench:
=========[![Maintainability](https://api.codeclimate.com/v1/badges/34f4377c5b911bcb4433/maintainability)](https://codeclimate.com/github/markburns/to_factory/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/34f4377c5b911bcb4433/test_coverage)](https://codeclimate.com/github/markburns/to_factory/test_coverage)
[![Gem Version](http://img.shields.io/gem/v/to_factory.svg)](https://rubygems.org/gems/to_factory)
[![License](http://img.shields.io/:license-mit-blue.svg)](http://markburns.mit-license.org)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` syntaxTested against Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.x
## Warning :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 :file_folder:
```ruby
#Gemfile
#add to whichever environments you want to generate data from
group :test, :development do
gem 'to_factory'
end
``````bash
git add spec/factories
git commit -m "I know what I am doing"
rails c
>ToFactory()
```
## Example :computer:```ruby
#Generate all factories
ToFactory()
#outputs the first record of each ActiveRecord::Base subclass in the models folder
#to spec/factories#Choose input/output directories
ToFactory.models = "models/this/subfolder/only" #default "./app/models"
ToFactory.factories = "spec/support/factories" #default "./spec/factories"
ToFactory()#Exclude classes
ToFactory(exclude: [User, Project])#Use Adhoc instances from the console
ToFactory User.last#writes to spec/factories/user.rb
FactoryBot.define
factory(:user) do |u|
email "[email protected]"
name "Mike"
end
end#List defined factory names
ToFactory.definitions
#=> [:user, :admin, :project]#Display definition from record
ToFactory.definition_for @user#Display existing definition from name
ToFactory.definition_for :admin#doesn't overwrite existing factories
ToFactory User.last
#Exception =>
#ToFactory::AlreadyExists: an item for each of the following keys :user already exists#Choose specific name
ToFactory :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:
* [rspec-kickstarter](https://github.com/seratch/rspec-kickstarter) (auto generate specs)
* [rspec-kickstarter-vintage](https://github.com/ifad/rspec-kickstarter-vintage) (for Ruby 1.8/Rspec 1.x)
* [hash_syntax](https://github.com/michaeledgar/hash_syntax) (convert 1.8 syntax to 1.9 and vice-versa)
* [transpec](https://github.com/yujinakayama/transpec) (convert old rspec syntax to new expect syntax)