RSpec Tutorial - D


Note - Very good link to setup factory bot gem in rails -- 

https://prabinpoudel.com.np/articles/setup-factory-bot-in-rails/


.........................................................


Note - jis time test case run karo tab byebug lagao uske controller par aagar controller ka test ca

se likh rahe ho toh . aur rspec run karo fir 


very good blog for spec in rails - see this blog also --


follow these below point - at 23 sep 2022 generate rspecs with this folder -- 


1. Add Gems - Add rspec-rails, factory_bot_rails,simplecov 

group :development do

  gem 'rspec-rails'

  gem 'factory_bot_rails'

  gem 'simplecov', require: false

end


2.

$ rails generate rspec:install

Note - This will create these 4 below Files -- 

      create  .rspec

      create  spec

      create  spec/spec_helper.rb

      create  spec/rails_helper.rb


3.

in spec folder - create controller models and other folders according to requirement 


4.create the coverage folder and index.html  

link :: https://www.youtube.com/watch?v=6qI5JHgYYUE  (youtube link ) 

Add this in the spec_helper.rb - at the top above RSpec.configure do |config| 

require 'simplecov'

SimpleCov.start



5.

give this command at terminal and it will genertate a colverage folder and index fiule inside the 

root folder 

rspec -fd



6. set the test database -

first in database.yml change the name of the database - 


...............................................................


old -----

 database: 21kschoolhrmsfinance-51700-ruby-second


changed -----

 database: 21kschoolhrmsfinance-51700-ruby-third 



..............................................................

test:

  <<: *default

  adapter: postgresql

  database: 21kschoolhrmsfinance-51700-ruby-third 

  username: postgres

  password: postgres

  host: <%= ENV['TEMPLATE_DATABASE_HOSTNAME'] %>

  port: 5432


...............................................................


and run the below commands -

command for create the test database - 
bundle exec rake db:create RAILS_ENV=test

command  to set the test enviornment for rspec-tests
rails db:environment:set RAILS_ENV=test

..........................................

..............................................................

......................................   ............................

.........................................

...........................



D – Rspec Tutorial ::

........................................


With New Rails Application ::


1. $ rails new rspec

2. $ rails s

3. $ rails g scaffold User title:string

4. $ Now come to github of rspec rails - https://github.com/rspec/rspec-rails

5. $ Now Add the rspec gem in your gemfile of the project : Inside developement add this gem

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

$bundle 

6. $ rails g rspec install - it will create the rspec

create .rspec

create spec

create spec/spec_helper.rb

create spec/rails_helper.rb

note :: above all create will not work – it will not take the command create



7. $ bundle update rspec-rails - it will update the rails bundle


8. $ rails g scaffold User


Commands to create and Test Spec files -


9. $ rails generate rspec:model user - it will create the spec file for model User


10. $ rails generate rspec:model user - create spec/models/user_spec.rb


11. $ bundle exec rspec - Run all spec files (i.e., those matching spec/**/*_spec.rb)


12. $ bundle exec rspec spec/models - Run all spec files in a single directory (recursively)


13.$ bundle exec rspec spec/controllers/accounts_controller_spec.rb - Run a single spec file


14. $    

bundle exec rspec spec/controllers/accounts_controller_spec.rb:8 - Run a single example


from a spec file (by line number)



15. Coverage File :: blog – dharmendrakushwah.1436.blogspot.com

We also insert one gem for the Coverage file :: semplecov gem


group :development, :test do

gem "debug", platforms: %i[ mri mingw x64_mingw ]

gem 'rspec-rails'

gem 'simplecov', require: false

end


$ bundle

........................................................

We will use this gem in the development/test do




16. Now We will work in spec_helper.rb :

require 'simplecov'


17. rspec - to test the rspecs

18. $ rspec spec/models

19. 

See the Coverage file index.html - spec/coverge/index.html – When open index.html it will

open in browser and show the rspec-testing status for all models , controllers , mailers etc



20.


Setup factory bot in rails ----


https://thedevpost.com/blog/setup-factory-bot-in-rails/


................................................................


rspec for post in chinching ----


context 'if user already like post' do

Post id is missing

context 'Post id is missing' do











given an expired token










describe 'DELETE bx_block_like_posts/like_posts' do

      context 'Removed from like.' do


      context 'given an invalid token' do








Comments

Popular posts from this blog

Rails 7 Features :: Comparison with Rails 6 and Rails 5