Search code examples
ruby-on-railsrubyunit-testingrspecrspec-rails

What are the pre-requisties that I need to run rspec for rails?


I have ruby on rails code of my team, now I need to do unit testing on the specs using rspec. I have gone through several websites but didn't get the perfect shot on how to use it. Can anyone please help me with the steps to use rspec. And also how to create an user in test db. Please guide me, I am new to rspec.


Solution

  • A good guide on setting up RSpec is gotten from the readme on the rspec repo page on GitHub..

    However... Personally, below is the combination I mostly use:

    1. Database cleaner: For cleaning the db as the name implies. A good guide on setting this up is as given by Avdi on his blog here.

    Note: This blog recommends putting the configuration for the database cleaner in a separate file in the support directory. If this is done, remember to uncomment the following line inside the rails_helper.rb file

    # Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
    

    This will ensure that all files under the support directory are required in the test suit.

    1. FactoryGirls: This is for generating test records(users and etc) just like you ask in the question. A good enough guide is as given on the getting started document here.
    2. Faker: I use this for generating Fake random values in building my records. E.g: first_name, last_name, email, password, etc for the test user instead of hard-coding these values. Using this is as direct as stated on the readme in the repo

    Properly armed with the above, the first Rspec specs is ready to be written.

    Specs can basically be divided into two steps:

    • Descriptions
    • Expectations

    A description is a summary of what the suit is testing for while an expectation is the normal expected outcome.

    Descriptions can also be nested in other descriptions.

    An example of an Rspec test is:

    require 'rails_helper'
    
    describe TextMessage do
      describe "#failed?" do
        it "returns true" do
          text = FactoryGirl.create(:text_message, :not_delivered)
          expect(text.send(:failed?)).to be_truthy
        end
      end
    end
    

    First, the rails_helper file was required. This is where all the test configuration are. This file is auto-generated when the rails generate rspec:install command is run from the Rspec installation guide on their repo.

    Next, I have a description block describe TextMessage do;end This is describing my model which is TextMessage in this case

    Next, I have another description block nested inside the first to describe a method (failed?) in my TextMessage model: describe "#failed?" do;end

    Then I have the expectation block which is my main test:

    it "returns true" do
      text = FactoryGirl.create(:text_message, :not_delivered)
      expect(text.send(:failed?)).to be_truthy
    end
    

    All other tests for the failed? method will be under this, with their respective blocks... while other tests for other methods in the model will be under the first one accordingly.

    Hope I have been able to throw in some light to the issue for you here.