• Stars
    star
    205
  • Rank 185,170 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

#set speeds up RSpec test suites by persisting records once

rspec-set <img src=“https://travis-ci.org/pcreux/rspec-set.svg?branch=master” alt=“Build Status” />¶ ↑

#set is a little RSpec helper that speeds-up drastically integration tests that relies on active record objects.

#set takes advantage of the fact that RSpec rails runs each examples in SQL transactions. Since all the changes made to the database are rolledback after each example we can create an active record object before all examples and use it in each examples without any collisions as long as we reload the object from the database before each example.

#set can be used as a replacement of #let: #set will create the resource before(:all) your examples and will reload the resource before(:each) example.

You can drastically improve the time spent to run your specs. On an application with 3000 examples we decreased the specs duration by 70%!

Usage¶ ↑

The following code will create one (and only one!) flight before running the examples and reload the flight from the DB before each example.

require 'spec_helper'

describe Flight do
  set(:flight) do
    Flight.create!
  end

  it "should be on_time" do
    flight.should be_on_time
  end

  it "should be cancellable" do
    flight.cancel
    flight.should be_cancelled
  end

  it "should be delayable" do
    flight.delay
    flight.should be_delayed
  end
end

How does that work?¶ ↑

RSpec wraps each example in an SQL transaction which gets rolled back at the end of each example.

#set creates a flight once before running any example. Each example uses this flight and changes its state. Since RSpec rolls back the SQL transaction, the flight gets back to its initial state before each example. #set takes care of reloading the flight from the DB before each example. Examples won’t affect each others then.

You can find more examples in github.com/pcreux/rspec-set/blob/master/features/lib/rspec-set.feature

Notes¶ ↑

  • #set works only with ActiveRecord objects saved to the DB so far.

  • The record created by #set won’t be deleted from the database. I encourage you to use DatabaseCleaner with the :truncation strategy to clean up your database. So far in RSpec 2.0, before(:all) runs before all describe/context blocks while after(:all) runs after every single describe/context/it blocks. Just make sure that you call DatabaseCleaner.clean in a before(:all) or after(:suite) then. :)

  • #set does not handle multi-level transactions.

  • You will have to call DatabaseCleaner.clean before(:all) specs which rely on having an empty database. #set don’t clean the database for you.

Install¶ ↑

Add rspec-set to you Gemfile

gem 'rspec-set'

and replace calls to #let creating active record objects by #set.

TODO¶ ↑

  • support non saved active record objects (changes made to non saved active record objects won’t be rolledback after each example)

  • make <tt>before(:all) running in a transaction - See: rhnh.net/2010/10/06/transactional-before-all-with-rspec-and-datamapper

  • support multi-level transactions (combinations of subcontext with set and changes made in before(:all) leads to weird behaviour sometimes

Contributing to rspec-set¶ ↑

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2010 Philippe Creux. See LICENSE.txt for further details.

More Repositories

1

csv-importer

CSV Import for humans on Ruby / Ruby on Rails
Ruby
583
star
2

pimpmychangelog

Pimp your CHANGELOG.md
Ruby
39
star
3

git-branch-delete-orphans

Delete tracking branches which remote branches do not exist anymore.
Ruby
17
star
4

aws-rotate-keys

One command to rotate your aws access keys
Ruby
15
star
5

gitmine

Last git commits with associated redmine ticket status.
Ruby
13
star
6

data-sncf-experiments

Experiments using SNCF open data
4
star
7

sncf2ical

Fetch voyages-sncf emails from your GMail inbox and create events in Google Calendar accordingly.
Ruby
3
star
8

thatz.at

Create a permalink to a date/time in your timezone. People will see it in their timezone.
Ruby
3
star
9

dmg

Ruby
3
star
10

vim

My vim config
Vim Script
3
star
11

slowdev.com

2
star
12

lyonrebuild

Grafted to https://github.com/lyonrb/lyonrb
JavaScript
2
star
13

facturation-pro-paybox

Integrate Facturation.pro with Paybox
Ruby
2
star
14

brother-scan-scripts

scripts for brother scanner
Shell
2
star
15

caw

Harsh cry tweets
Ruby
2
star
16

dmg-pkgs

Packages for dmg
2
star
17

engineering

Our Engineering Principles and Practices
Ruby
2
star
18

eolv.fr

Europe Oenologie Les Verres
2
star
19

SimpleSampler

A Simple Sampler playing a sound when you push its button. Ya, I said Simple. :)
Ruby
2
star
20

refuge

Social network for cool coworking places
JavaScript
1
star
21

test-pimp-my-changelog

1
star
22

ti.cx

HTML
1
star
23

locate-images

Ruby
1
star
24

science-world-solar-system

A Solar System where the Science World represents the Sun.
Ruby
1
star
25

coding-dojo-ruby-yahtzee

Coding dojo ran at IUT Informatique Aix-en-Provence on April 9th, 2013. #ruby
Ruby
1
star