Giter Site home page Giter Site logo

factory_girl-seeds's Introduction

Code Climate

FactoryGirl Seeds

Don't like factory_girl because it is slow? Do you know that creating records in DB through factory_girl can take up to 50% of total spec run time? And even more!

This tiny gem helps fix that problem by reusing data preloaded before running test suite.

Benchmark

Without seeds:

Finished in 4 minutes 38.7 seconds
1402 examples, 0 failures, 0 pending

With seeds:

Finished in 2 minutes 40.6 seconds
1402 examples, 0 failures, 0 pending
>> (2.minutes + 40.6.seconds) / (4.minutes + 38.7.seconds)
=> 0.5762468604233943

So it is just about 58% of time before seeds optimization :)

Installation

Add this line to your application's Gemfile:

group :test do
  gem 'factory_girl-seeds'
end

Usage

1. Create records before test suite

FactoryGirl::SeedGenerator.create(:user, name: "Carlos Castaneda")

For example if you are using rspec then add this to config.before(:suite).

2. Use in factory definitions

This is the most important step because most of time factory_girl spends on creating associations which in turn also create associations and so on recursively.

FactoryGirl.define do
  factory :post do
    title "Demo"
    user { seed(:user) }
  end
end

3. Use in it blocks.

Also if you need standard factory without overriding attributes then do not create records. Just use one from preloaded seeds.

it "should do something" do
  user = FactoryGirl.seed(:user)

  # your code here
end

Short DSL also available:

user = seed(:user)

How it works?

FactoryGirl::SeedGenerator.create method creates record in DB before transaction begins. Then it block starts transaction so when you update record returned by FactoryGirl.seed it is wrapped in transaction. This guarantees that every it block works with clean record.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

factory_girl-seeds's People

Contributors

divineforest avatar kirs avatar makaroni4 avatar aerlinger avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.