Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 85 lines (62 sloc) 2.794 kb
b4560eb David Chelimsky relish docs
dchelimsky authored
1 When you run `rails generate rspec:install`, the `spec/spec_helper.rb` file
2 includes the following configuration:
3
4 RSpec.configure do |config|
5 config.use_transactional_fixtures = true
6 end
7
8 The name of this setting is a bit misleading. What it really means in Rails
9 is "run every test method within a transaction." In the context of rspec-rails,
10 it means "run every example within a transaction."
11
12 The idea is to start each example with a clean database, create whatever data
13 is necessary for that example, and then remove that data by simply rolling back
14 the transaction at the end of the example.
15
16 ### Disabling transactions
17
18 If you prefer to manage the data yourself, or using another tool like
19 [database_cleaner](https://github.com/bmabey/database_cleaner) to do it for you,
20 simply tell RSpec to tell Rails not to manage transactions:
21
22 RSpec.configure do |config|
23 config.use_transactional_fixtures = false
24 end
25
26 ### Data created in `before(:each)` are rolled back
27
28 Any data you create in a `before(:each)` hook will be rolled back at the end of
29 the example. This is a good thing because it means that each example is
30 isolated from state that would otherwise be left around by the examples that
31 already ran. For example:
32
33 describe Widget do
34 before(:each) do
35 @widget = Widget.create
36 end
37
38 it "does something" do
39 @widget.should do_something
40 end
41
42 it "does something else" do
43 @widget.should do_something_else
44 end
45 end
46
47 The `@widget` is recreated in each of the two examples above, so each example
48 has a different object, _and_ the underlying data is rolled back so the data
49 backing the `@widget` in each example is new.
50
51 ### Data created in `before(:all)` are _not_ rolled back
52
53 `before(:all)` hooks are invoked before the transaction is opened. You can use
54 this to speed things up by creating data once before any example in a group is
55 run, however, this introduces a number of complications and you should only do
56 this if you have a firm grasp of the implications. Here are a couple of
57 guidelines:
58
59 1. Be sure to clean up any data in an `after(:all)` hook:
60
61 before(:all) do
62 @widget = Widget.create!
63 end
64
65 after(:all) do
66 @widget.destroy
67 end
68
69 If you don't do that, you'll leave data lying around that will eventually
70 interfere with other examples.
71
72 2. Reload the object in a `before(:each)` hook.
73
74 before(:all) do
75 @widget = Widget.create!
76 end
77
78 before(:each) do
79 @widget.reload
80 end
81
82 Even though database updates in each example will be rolled back, the
83 object won't _know_ about those rollbacks so the object and its backing
84 data can easily get out of sync.
Something went wrong with that request. Please try again.