Set up RSpec

Add RSpec to your Gemfile

Put rspec-rails in your Gemfile under your development and test groups. While you’re here, let’s also use some debugger gems -- byebug is there by default, but let's also add pry. We’ll use those later when writing and debugging tests.

group :development, :test do
  # Our brave brigade of debuggers!
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] # from rails new
  gem 'pry'

  # RSpec & testing gems!
  gem 'rspec-rails', '~> 3.7'
  gem 'shoulda-matchers', '~> 3.1'

  # For test data generation
  gem "factory_bot_rails", "~> 4.0"
  gem 'faker', :git => 'https://github.com/stympy/faker.git', :branch => 'master'
end

Run bundle install to install the gems.

Generate RSpec files

Then, in your console, run...

rails generate rspec:install

Let's check out what that generated (diff here) ...

.rspec
spec/rails_helper.rb
spec/spec_helper.rb

Configure RSpec

Let's open the spec_helper.rb file... note the commented out block at the end of the file. Let's move some of these into our config block to uncomment.

  • more verbose formatting on individual test runs
  • look at how fast/slow your tests are
  • randomize the order of your test -- sniffs out order related dependencies
  • seed randomization -- allows you to reproduce randomization related failures!
  # Many RSpec users commonly either run the entire suite or an individual
  # file, and it's useful to allow more verbose output when running an
  # individual spec file.
  if config.files_to_run.one?
    # Use the documentation formatter for detailed output,
    # unless a formatter has already been configured
    # (e.g. via a command-line flag).
    config.default_formatter = "doc"
  end

  # Print the 10 slowest examples and example groups at the
  # end of the spec run, to help surface which specs are running
  # particularly slow.
  config.profile_examples = 10

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = :random

  # Seed global randomization in this process using the `--seed` CLI option.
  # Setting this allows you to use `--seed` to deterministically reproduce
  # test failures related to randomization by passing the same `--seed` value
  # as the one that triggered the failure.
  Kernel.srand config.seed

results matching ""

    No results matching ""