Rails: How to setup rspec on Rails

  1. Add rspec to the Gemfile;

     group :development, :test do
       gem 'rspec-rails'
     end
    

  2. Install the dependencies

     bundle install
    
  3. Check if it was installed correctly

     gem list rspec
    

    You should see something like this

  4. Check version

  5. Install rspec

     rails generate rspec:install
    

You should see something like this

  1. Generate a rspec model

     rails generate rspec:model Product
    

    Product is a model. This is just an example.

  2. Destroy a rspec model (Just in case you need it)

     rails destroy rspec:model Product
    

  3. Run a specific test on a single file

     rspec spec/models/product_spec.rb:3
    

    In this example, the test starts in line 3.

    You should see something like this

  4. Run the tests for a single file

     rspec spec/models/product_spec.rb
    

    You should see something like this

  5. Run all tests

     rspec spec
    

    You should see something like this

  6. Celebrate! It's working.


Conclusion

In this article, we learned how to setup Rspec on Rails.

Let me know if you need any additional help.