#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%!
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
#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.
Add rspec-set to you Gemfile
and replace calls to #let creating active record objects by #set.
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.