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