GithubHelp home page GithubHelp logo

pierre-boutbel / transactionata Goto Github PK

View Code? Open in Web Editor NEW

This project forked from colszowka/transactionata

0.0 2.0 0.0 102 KB

Transactionata: Transactional dynamic test data for Rails Tests. Set up your models and factories in a block that will be executed once and then rolled back by hooking into ActiveRecord's built-in fixture transactions

Home Page: http://rubygems.org/gems/transactionata

License: MIT License

transactionata's Introduction

Transactionata

Transactional dynamic test data for rails

Synopsis

Fixture replacements like Factory Girl are great, but when you have to set up complex test data or use Shoulda and your test records are created for each should block, you’ll get a huge performance hit.

Rails’ Fixtures are loaded only once and then rolled back via database transaction after every test - why not leverage this for your Factory-built test data as well? Transactionata allows you to do so:

class UserTest < ActiveSupport::TestCase
  test_data do
    Factory.create(:user, :login => 'colszowka')
    # super-complex, time consuming, web-service-hooked-up test data setup continues
  end

  should "destroy a User" do
    assert User.find_by_login('colszowka').destroy
  end

  should "have a User" do
    assert User.find_by_login('colszowka')
  end

  context "The User" do
    subject { @user ||= User.find_by_login!('colszowka') }

    should "not be a new record" do
      assert !subject.new_record?
    end
  end
end

This will run the code block given to test_data alongside Fixtures and therefore ‘protect’ your data using Rails built-in fixture transactions, giving you huge performance gains if you’re using factories a lot.

For the (FactoryGirl/Shoulda-heavy) Ruby Toolbox 2 test suite, the time for a complete test run went from 5 minutes to 50 seconds by just moving test data generation into centralized blocks for each test case using Transactionata.

Usage

Add it to your Gemfile and bundle install.

gem 'transactionata', :group => :test

Then move test data generation to a test_data block as shown above, using finders instead of creating the records in the setup/subject blocks.

Of course, all of this is completely applicable to default Ruby/Rails unit tests, you don’t have to use Shoulda for transactional data setup to work.

Please note that you’ll have to have fixture files for the models you’re using transactionata for. They can be empty, but they have to be in place so Rails’ Fixture loading mechanism will purge the database tables between multiple test cases.

Compatibility

Transactionata’s test suite currently runs against Rails 2.3.10 and 3.0.4 on Ruby 1.9.2, 1.8.7 and REE. Since no black magic is involved, it should work on other platforms and versions as well.

Copyright © 2011 Christoph Olszowka. See LICENSE for details.

transactionata's People

Contributors

colszowka avatar

Watchers

Pierre Boutbel avatar 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.