Once rspec-rails is installed, you’ll be able to run your spec suite using either bundle exec rspec or bin/rake spec. Commands in the bin directory, like rake or rails, are Rails-generated binstubs (wrapper scripts) that save you the hassle of remembering to type bundle exec before every command.[127]
If you’re used to creating binstubs via Bundler’s --binstubs option as described in Bundler, be aware that this option may not play nicely with Spring, a Rails preloader designed to speed up boot times.[128] You may see Rails commands hang or print warning messages from the Bundler-generated binstubs. To solve the problem, you’ll need to either remove Spring or switch to the binstubs provided by Rails:
| $ bundle config --delete bin |
| $ rails app:update:bin |
When you generate a Rails object such as a controller or scaffold, RSpec will create a corresponding spec file for you:
| $ rails generate model pterodactyl |
| invoke active_record |
| create db/migrate/20170613203526_create_pterodactyls.rb |
| create app/models/pterodactyl.rb |
| invoke rspec |
| create spec/models/pterodactyl_spec.rb |
You can also generate just the spec file, if the class you’re testing already exists or if you don’t want to create it just yet; just prepend the item you’re generating with rspec:, as in rails generate rspec:model pterodactyl.
Next, let’s take a look at the different types of specs you might write to test your Rails app.