Rspec - Rails == Fully certified by dwijendra ::
.......................................................................................................
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
note - but in hrms this code is writeen in the rails_helper.rb
5. MMMMM IMP - without this command converage file will not be generated
give this command at terminal and it will genertate a colverage folder and index file inside the
root folder
rspec -fd
before this above command -
if test database is not created then system will say for it --
rails db:migrate RAILS_ENV=test
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
..........................................
How to run rspec --
take the file path from right click in the particular file of rspec ::
bundle exec rspec
bundle exec rspec spec/controllers/accounts_controller.rb
..........................................
Check the status of Rspec ::
Note :: Check coverage folder in the root database and open index.html file insdie that ::
note - coverage folder root folder ke andar hota hai aur uske andar index.html file hoti hai jo browser par khulti hai - isse hum check kar sakte hein rspec ke status ko
note - lekin controllers/ models everything dikhai dena chahiyeh agar aisa nahi hai aur sirf all spec dikhai de rahe hein means kuch error hain
..........................................
Different Rspecs ::
1.
Check Response Status
expect(response.status).to eq 201
2.
Check Response
json_data = json_response(response)
3.
Match id in Update Method ::
expect(json_data['data']['id'].to_i).to eq @tax.id
4.
Check Error
expect(json_data["errors"]['name'].join("")).to eq "can't be blank"
5.
Check Error Status
expect(response).to have_http_status 422
6.
In filter Method ::
expect(json_data["data"].count).to eq BxBlockTax::Tax.count
Note -
1. Get ke sath params nai ayega rspecs mein --
2. params mein kya data dalna hai uske liye postman se utha sakte ho
7.
For write the first rspec test case first we write the command at terminal -
generally request folder is used for the controller in rails
$ rails generate rspec:request Customer
7.
Imp points rspecs ::
...............................................................................................................
1. postman ki api se body lena
2. postman se api se route lena
3. module aur class ka name upar theek se likhna
4. agar kisi ki schema mein optional laga hai toh usko hata sakte hein
5. json ki form mein data ko lene ke liye hum kya karte hein ---
json_response(response)
6. data ko kaise lete hein output mein byebug lagakar spec bundle ko chalakar command ko -
response.body
response.status
response
7.message aise check karate
8. byebug lagate controller mein aur rspec mein bhi aur fir rspec ki ciommand chalate
controller mein pehle byebug rukta fir rspec mein byebug rukta , controller mein jo byebug rukta hai usmein data ko params se check karte hein ki params mein kya aa raha hai
9. Routes ko is tarah se banayenge ------------------------------------
AcademicAccount_URL = '/bx_block_academic_account/academic_account/'
Note :: Starting aur last dono mein hi / lagate hein nahi toh error ayegi
...............................................................................................................................................
Note ::
very important --
jis kisi ka bhi spec ka status index.html (coverage folder ki file ) mein dekhna hai uska pehle spec chala na padhega command line par
$ bundle exec rspec {filepathname}
and then uska status show hoga coverage file par (index.html.erb ) par ::
...............................................................................................................................................
...............................................................................................................................................
FEE_URL = ' /bx_block_fee/fees/' - bad url error will give
invalid uri ki error deta hai agar last mein / nahi lagaya hai toh
FEE_URL = '/bx_block_fee/fees/' - correct
FEE_URL = '/bx_block_fee/fees - will give error
starting mein / lagana hai
end mein / lagana hai
nahi toh url ki error dega
Note -- so choose the correct accurate syntax always
...............................................................................................................................................
...............................................................................................................................................
good links ::
https://www.google.co.in/search?q=how+to+write+the+spec+with+no+records+for+rspecs+&sxsrf=ALiCzsYLDVZf2HkXFF9CfYApV-aqYoYpTA%3A1666179684305&source=hp&ei=ZOJPY4_tD4ScseMPpNWTsAI&iflsig=AJiK0e8AAAAAY0_wdKYiQKtOtQMzXQu-Rs3xlODFIkse&ved=0ahUKEwiPr9mBm-z6AhUETmwGHaTqBCYQ4dUDCAg&uact=5&oq=how+to+write+the+spec+with+no+records+for+rspecs+&gs_lcp=Cgdnd3Mtd2l6EAMyBwghEKABEAoyBwghEKABEAoyBwghEKABEAo6BAgjECc6BQgAEJECOgsIABCABBCxAxCDAToOCC4QgAQQsQMQgwEQ1AI6CAgAELEDEIMBOgoIABCxAxCDARAKOggIABCABBCxAzoLCC4QgAQQsQMQgwE6BQgAEIAEOgsILhCABBDHARDRAzoICC4QgAQQsQM6BQguEIAEOgUIABCxAzoGCAAQFhAeOggIIRAWEB4QHToFCCEQoAE6CgghEBYQHhAPEB06CAgAEBYQHhAPUABYmVFg8VNoAHAAeACAAZcCiAHYPZIBBjAuNDIuN5gBAKABAQ&sclient=gws-wiz
...............................................................................................................................................
Command to check the status of Rspec in the Current Project at terminal ::
$ rails stats
..................................................................
Write Data in First Spec ::
1. in request folder create -
folder - bx_block_tax
in folder bx_block_tax -file - tax_spec.rb
2. in factories folder create - tax.rb --
..................................................................
Testing of rspec ::
1. rspec mein byebug lagao aur rspec chalao terminal par --
byebug lagta hai it mein --
it "when tax successfully Created" do
byebug
post TAX_URL , params: params, headers: @headers
json_data = json_response(response)
expect(response).to have_http_status 201
end
or
byebug kahan par kis jagah par lagana cahiyeh isko deko
............................................................................................................
lagega byebug --
............................................................................................................
describe "Get#index" do
byebug
it 'returns list of Taxes' do
get TAX_URL, headers: @headers
json_data = json_response(response)
expect(response.status).to eq 200
end
............................................................................................................
............................................................................................................
lagega byebug --
............................................................................................................
describe "POST#create" do
byebug
let(:params) do {
tax: {
name: Faker::Name.name,
tax_percentage: 15,
description: "tax1 description",
tax_type: "CGST",
is_gst: true
}
}
end
............................................................................................................
require 'rails_helper'
RSpec.describe "BxBlockTax::Tax", type: :request do
TAX_URL = '/bx_block_tax/taxes/'
TAX_URL_FILT = '/bx_block_tax/taxes/filter'
TAX_URL_IMP = '/bx_block_tax/taxes/import'
TAX_URL_EXP = '/bx_block_tax/taxes/export'
TAX_URL_BULK_U = '/bx_block_tax/taxes/bulk_update'
TAX_URL_Sh = '/bx_block_tax/taxes/search'
TAX_URL_Namerates = '/bx_block_tax/taxes/tax_rate_names/'
before(:all) do
role = BxBlockRolesPermissions::Role.find_or_create_by(name: "Director")
@account = create(:account, role_id: role.id)
auth_token = BuilderJsonWebToken::JsonWebToken.encode(@account.id)
@account.update(lms_token: auth_token)
@headers = {
"token" => auth_token
}
@tax = create(:tax)
@tax2 = create(:tax)
@tax3 = create(:tax)
@tax4 = create(:tax, tax_type:"CGST")
end
describe "Get#index" do
byebug
it 'returns list of Taxes' do
get TAX_URL, headers: @headers
json_data = json_response(response)
expect(response.status).to eq 200
end
# it 'no records' do
# get TAX_URL, headers: @headers,taxes: @taxes
# json_data = json_response(response)
# expect(response.status).to eq 200
# end
end
describe "POST#create" do
let(:params) do {
tax: {
name: Faker::Name.name,
tax_percentage: 15,
description: "tax1 description",
tax_type: "CGST",
is_gst: true
}
}
end
let(:taxtype_params) do {
tax: {
name: Faker::Name.name,
tax_percentage: 15,
description: "tax1 description",
tax_type: "",
is_gst: true
}
}
end
let(:valid_taxtype_params) do {
tax: {
name: Faker::Name.name,
tax_percentage: 15,
description: "tax1 description",
tax_type: "GST",
is_gst: true
}
}
end
let(:name_params) do {
tax: {
name: "",
tax_percentage: 15,
description: "tax1 description",
tax_type: "CGST",
is_gst: true
}
}
end
let(:tax_per_params) do {
tax: {
name: Faker::Name.name,
tax_percentage: "" ,
description: "tax1 description",
tax_type: "CGST",
is_gst: true
}
}
end
it "when tax successfully Created" do
post TAX_URL , params: params, headers: @headers
json_data = json_response(response)
expect(response).to have_http_status 201
end
it "Please select tax type" do
post TAX_URL , params: taxtype_params, headers: @headers
json_data = json_response(response)
expect(json_data["errors"]["message"].join("")).to eq "Please select tax type"
expect(response).to have_http_status 422
end
.................................
upar ke code ke liye pehle humein
@account ko dekhna padhega
............................
Was Error in Rspec create -
data is not emply
account hi create nahi ho raha tha
account.rb ki factory mein gaye fir poora sabhi columns wahan ke console par chalakar
BxBlockAccount::Account.create!() -- isko chalaya
r sir --
lms tolken mein auth_token save karwana padhega --
@account.update(lms_token: auth_token)
note:
describe "Get#index" do
it 'returns list of Taxes' do
get TAX_URL, headers: @headers
json_data = json_response(response)
byebug
expect(response.status).to eq 200
end
maximum case mein response ke baad byebug lagate hein taki data mile
Comments
Post a Comment