rspec/rspec-rails — Issue #496: Support for transactional/truncation in before(:all) blocks.
Author: stevenharman
Created: 2012-01-24 15:59:27+00:00
State: open

As an example, see [rspec-set] which adds a `set(:foo) { ... }` syntax that works much like `let(:foo) { ... }` except within a `before(:all)` block. This leads to much faster model specs as you can create a particular state once and then have a number of `it` blocks making independent assertions. In particular, I've found this very useful when triangulating a named scope, etc.

As rspec-set exists today, it is left as an exercise to the consumer to clean up any records created via the `set` block, but this doesn't have to be so. For example, @xaviershay has done this for Datamaper. [datamapper-set]

I also realize this can quickly get complicated once nested contexts are rolled into the mix, and that different databases support various flavors of nested transactions. I've addressed the later issue with `DatabaseCleaner.strategey = :truncation`. The first issue may require more thought, or perhaps is just not worth supporting?

[rspec-set]: https://github.com/pcreux/rspec-set
[datamapper-set]: http://rhnh.net/2010/10/06/transactional-before-all-with-rspec-and-datamapper