Setup Rspec

 1 install following Gems    

  group :development, :test do    

    # Use for test application

      gem 'rspec-rails', '~> 5.0.2'

      # Use for fixtures

      gem 'factory_bot_rails', '~> 5.2.0'

      # For create fake data

      gem 'faker', '~> 2.19.0'

      # use assigns in controller rspec

      gem 'rails-controller-testing'

  end   

      

  bundle install    

  rails generate rspec:install    

  bundle exec rspec   

      

2 Format Spec output on console   

  add below line in  spec/.rspec file (create new file .rspec)    

  --format documentation    

      

3 Modal Test cases    

  rails g rspec:model user    

      

4 controller Test cases   

  rails g rspec:controller users    

      

      

5 Use simplecov gem for check test case coverage code   

  https://github.com/colszowka/simplecov    

  OR    

  type following command    

  rails stats   

      

6  factory_bot_rails    

  this gem is used for generate test data. It is latest version of factory girl   

  setup steps   

  1 create support folder under spec and create new file there factory_bot.rb 

    spec/support/factory_bot.rb 

    require 'factory_bot' 

    RSpec.configure do |config| 

      config.include FactoryBot::Syntax::Methods  

    end 

      

  2 spec/rails_helper.rb  

    require 'support/factory_bot' 

      

7 run particular model’s specs    

  rspec spec/models/system_spec.rb    

  run particular controller specs   

  rspec spec/controllers/nagios_checks_controller_spec.rb     

  run particular spec by line   

  rspec spec/models/system_spec.rb:55   

  run particular multiple specs by line numbers   

  rspec spec/models/system_spec.rb:30:60    

      

8 controller spec notes   

  Note 1  when we use assigns(:systems) it means it is taking systems controllers @systems variable. If we change variable there, we must update this variable as well  

    example:          expect(assigns(:systems)).not_to eq(System.all) 

      

      

  Note 2  Use render template then action while rendering 

    example:          expect(response).to render_template(:index)

                expect(response).to render_template(:new)

      

  Note 3  Use redirect template then route  when redirecting  

    Use redirect when we moving from 1 screen to another same as our real controller code 

    example:  expect(response).to redirect_to(systems_path)


Comments

Popular posts from this blog

Standard of Code Sequence of model

Difference between was, before_last_save, changed and changed?