|
1 |
| -# Upgrading from rspec-rails-2.x to rspec-rails-3 |
| 1 | +# Upgrading from rspec-rails 3.x to version 4 |
2 | 2 |
|
3 |
| -For detailed information on the general RSpec 3.x upgrade process see the [RSpec |
4 |
| -Upgrade docs](https://relishapp.com/rspec/docs/upgrade). |
| 3 | +RSpec Rails 4 is a major version under semantic versioning, it allowed us to change the supported Rails versions to 5 and 6 only. There are no changes required to upgrade to RSpec Rails 4 if you are using Rails 5 or 6. |
5 | 4 |
|
6 |
| -There are several changes specific to `rspec-rails` to be aware of: |
| 5 | +If you are using Rails 4.2 you can use RSpec Rails 4, but note that support for it is not maintained, we consider this a breaking change hence the version change, and you must be on Ruby 2.2 as a minimum. |
7 | 6 |
|
8 |
| -- [Default helper files created in RSpec 3.x have changed](#default-helper-files) |
9 |
| - |
10 |
| -- [File-type inference disabled by default](#file-type-inference-disabled) |
11 |
| - |
12 |
| -- [Rails 4.x `ActiveRecord::Migration` pending migration checks](#pending-migration-checks) |
13 |
| - |
14 |
| -- [Extraction of `stub_model` and `mock_model` to `rspec-activemodel-mocks`](#extract-stub-model) |
15 |
| - |
16 |
| -<a name="default-helper-files"></a> |
17 |
| -## Default helper files created in RSpec 3.x have changed |
18 |
| - |
19 |
| -In prior versions, only a single `spec_helper.rb` file was generated. This file |
20 |
| -has been moved to `rails_helper.rb`. The new `spec_helper.rb` is the same |
21 |
| -standard helper generated by running `rspec --init`. |
22 |
| - |
23 |
| -This change was made to accomplish two general goals: |
24 |
| - |
25 |
| -- Keep the installation process in sync with regular RSpec changes |
26 |
| - |
27 |
| -- Provide an out-of-the-box way to avoid loading Rails for those specs that do |
28 |
| - not require it |
29 |
| - |
30 |
| -<a name="generators"></a> |
31 |
| -### Generators |
32 |
| - |
33 |
| -Generators run in RSpec 3.x will require `rails_helper` and not `spec_helper`. |
34 |
| - |
35 |
| -<a name="upgrading-an-existing-app"></a> |
36 |
| -### Upgrading an Existing App |
37 |
| - |
38 |
| -For most existing apps, one of the following upgrade paths is sufficient to |
39 |
| -switch to the new helpers: |
40 |
| - |
41 |
| -#### I need to move things over in stages |
42 |
| - |
43 |
| -1. Create a new `rails_helper.rb` with the following content: |
44 |
| - |
45 |
| - ```ruby |
46 |
| - require 'spec_helper' |
47 |
| - ``` |
48 |
| - |
49 |
| -2. As necessary, replace `require 'spec_helper'` with `require 'rails_helper'` |
50 |
| - in the specs. |
51 |
| - |
52 |
| -3. When ready, move any Rails specific code and setup from `spec_helper.rb` to |
53 |
| - `rails_helper.rb`. |
54 |
| - |
55 |
| -#### I'm ready to just switch completely |
56 |
| - |
57 |
| -1. Move the existing `spec_helper.rb` to `rails_helper.rb`: |
58 |
| - |
59 |
| - ```ruby |
60 |
| - git mv spec/spec_helper.rb spec/rails_helper.rb |
61 |
| - ``` |
62 |
| - |
63 |
| -2. Run the installation rake task opting to not replace `rails_helper.rb`: |
64 |
| - |
65 |
| - ```console |
66 |
| - $ bin/rails generate rspec:install |
67 |
| - create .rspec |
68 |
| - exist spec |
69 |
| - create spec/spec_helper.rb |
70 |
| - conflict spec/rails_helper.rb |
71 |
| - Overwrite my_app/spec/rails_helper.rb? (enter "h"for help) [Ynaqdh] n |
72 |
| - skip spec/rails_helper.rb |
73 |
| - ``` |
74 |
| - |
75 |
| -3. Move any non-Rails RSpec configurations and customizations from your |
76 |
| - `rails_helper.rb` to `spec_helper.rb`. |
77 |
| - |
78 |
| -4. Find/replace instances of `require 'spec_helper'` with |
79 |
| - `require 'rails_helper'` in any specs which rely on Rails. |
80 |
| - |
81 |
| -<a name="file-type-inference-disabled"></a> |
82 |
| -## File-type inference disabled by default |
83 |
| - |
84 |
| -Previously we automatically inferred spec type from a file location, this |
85 |
| -was a surprising behaviour for new users and undesirable for some veteran users |
86 |
| -so from RSpec 3 onwards this behaviour must be explicitly opted into with: |
87 |
| - |
88 |
| -```Ruby |
89 |
| -RSpec.configure do |config| |
90 |
| - config.infer_spec_type_from_file_location! |
91 |
| -end |
92 |
| -``` |
93 |
| - |
94 |
| -This change was made to accomplish our general goals of acting with the principle |
95 |
| -of least surprise and making RSpec configuration more explicit. See [the |
96 |
| -directory structure documentation](https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/directory-structure) for more details. |
97 |
| - |
98 |
| -<a name="pending-migration-checks"></a> |
99 |
| -## Rails 4.x `ActiveRecord::Migration` pending migration checks |
100 |
| - |
101 |
| -If you are not using `ActiveRecord` you do not need to worry about these |
102 |
| -settings. |
103 |
| - |
104 |
| -Users of Rails 4.x can now take advantage of improved schema migration and sync |
105 |
| -abilities. Prior to RSpec 3, users were required to manually run migrations in |
106 |
| -both the development and test environments. Additionally, the behavior differed |
107 |
| -depending on if the specs were run via `rake` or via the standalone `rspec` |
108 |
| -command. |
109 |
| - |
110 |
| -With the release of Rails 4, new APIs have been exposed on |
111 |
| -`ActiveRecord::Migration`. This allows RSpec to take advantage of these new |
112 |
| -standard migration checks, mirroring behavior across the board. |
113 |
| - |
114 |
| - - Rails 4.0.x |
115 |
| - |
116 |
| - Add the following to the top of the `rails_helper` file after Rails has |
117 |
| - been required: |
118 |
| - |
119 |
| - ```ruby |
120 |
| - ActiveRecord::Migration.check_pending! |
121 |
| - ``` |
122 |
| - |
123 |
| - This will raise an exception if there are any pending schema changes. Users |
124 |
| - will still be required to manually keep the development and test |
125 |
| - environments in sync. |
126 |
| - |
127 |
| - - Rails 4.1+ |
128 |
| - |
129 |
| - With this release there was an exciting new feature. Users no longer need |
130 |
| - to keep the development and test environments in sync. To take advantage of |
131 |
| - this add the following to the top of the `rails_helper` file after Rails |
132 |
| - has been required: |
133 |
| - |
134 |
| - ```ruby |
135 |
| - ActiveRecord::Migration.maintain_test_schema! |
136 |
| - ``` |
137 |
| - |
138 |
| - What this does is that rather than just raising when the test schema has |
139 |
| - pending migrations, Rails will try to load the schema. An exception will |
140 |
| - now only be raised if there are pending migrations afterwards the schema |
141 |
| - has been loaded. |
142 |
| - |
143 |
| - There are a few caveates to be aware of when using this: |
144 |
| - |
145 |
| - - Migrations still need to be run manually; although now this only has to |
146 |
| - be done in the 'development' environment |
147 |
| - - An exception will be raised If the schema has not been initialized. The |
148 |
| - exception will provide instructions stating `rake db:migrate` needs to |
149 |
| - be run. |
150 |
| - |
151 |
| -It is possible to opt-out of checking for pending migrations. Since this is |
152 |
| -actually a feature of Rails, the change needs to be done as part of the Rails |
153 |
| -configuration. To do this, add the following to your |
154 |
| -`config/environments/test.rb` file: |
155 |
| - |
156 |
| -```ruby |
157 |
| -config.active_record.maintain_test_schema = false |
158 |
| -``` |
159 |
| - |
160 |
| -New RSpec projects don't need to worry about these commands as the `rails |
161 |
| -generate rspec:install` will add them automatically. |
162 |
| -
|
163 |
| -<a name="extract-stub-model"></a> |
164 |
| -## Extraction of `stub_model` and `mock_model` to [`rspec-activemodel-mocks`](https://github.com/rspec/rspec-activemodel-mocks) |
165 |
| -
|
166 |
| -Historically, `stub_model` and `mock_model` have been difficult to maintain. |
167 |
| -They are tightly coupled to `ActiveRecord` which isn't always the ORM of choice. |
168 |
| -This maintainence coupling has lead to delays with previous releases. |
169 |
| -Additionally, the objects generated by these methods hide important |
170 |
| -`ActiveRecord` behavior complexity which would otherwise be good to expose. |
171 |
| - |
172 |
| -Some alternatives are: |
173 |
| - |
174 |
| -- Wrap calls to `ActiveRecord` objects in more specific domain models and |
175 |
| - services |
176 |
| -- Use new unsaved `ActiveRecord` instances (e.g. `Model.new`) |
177 |
| -- Consider [partial mocks](https://www.relishapp.com/rspec/rspec-mocks/docs/basics/partial-test-doubles) on an `ActiveRecord` instance |
178 |
| -- Let the specs hit database directly where appropriate |
| 7 | +If you are upgrading from an earlier version of RSpec Rails, please consult [the upgrading 2.x to 3.x guide](https://relishapp.com/rspec/rspec-rails/v/3-9/docs/upgrade). |
0 commit comments