ChingChing App RSpec for firebase_notification_controller

 Write Rspec :: R

In ChingChing Project :: Firebase Controller ::

push_notification_status_controller ::


Note : We should also check the callback means before_action -that all the methods of the current user is using or not .

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


Step 1 :: For which Controller you are doing the Rspec -

Check the type of the method in route -

route.rb – search

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

namespace :bx_block_notifications do

resources :notifications, :only => [:index, :show, :create, :destroy, :update]

resources :subscribed_tips_nofication, :only => [:index, :show, :create, :destroy, :update]

get 'read_status', to: "notifications#read_status"

resources :notification_status, :only => [ :show, :create, :update]

post 'triggering_firebase_notification' => 'firebase_notifications#triggering_firebase_notification'

post 'fcm_token_update' => 'firebase_notifications#fcm_token_update'

post 'onesignal_player' => 'firebase_notifications#onesignal_player'

resources :email_notifications, :only => [:index]

end

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


Here I get the type POST :: Ismein type POST hai

ismein already 1 method ka likha hua tha usko copy kiya ::



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


Step 2 ::


Now I will see the Controller Code ::


def fcm_token_update

update_fcm = current_user.update(fcm_token: params["fcm_token"])

if update_fcm

render json: AccountBlock::EmailAccountSerializer.new(current_user).serializable_hash, status: :ok

else

return render json: {message: "Something Went Wrong"}

end

end

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

We will understand the code ::

1. fcm token is created in params

2. current user will update through fcm_token

3. We will get the current_user by the fcm_token

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


Step 3 ::


First Created Account give the current user , token is passed inside the header and the current user token pass inside the header.

Step 4 ::

We set the endpoint for the particular method – by this it will go to route ::

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


describe 'POST /bx_block_notifications/fcm_token_update' do

let(:endpoint) { "/bx_block_notifications/fcm_token_update" }

let(:params) {{ fcm_token: "testing" }}

before { post endpoint, :headers => headers, :params => params }

it 'Update FCM token' do

expect(response.status).to eq 200

expect(json["data"]["id"].to_i).to eq account1.id

end

end


Step 5 ::

Copy the Code of One Rspec and then change it in your code ::

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


describe 'POST /bx_block_notifications/fcm_token_update' do

let(:endpoint) { "/bx_block_notifications/fcm_token_update" }

let(:params) {{ fcm_token: "testing" }}

before { post endpoint, :headers => headers, :params => params }

it 'Update FCM token' do

expect(response.status).to eq 200

expect(json["data"]["id"].to_i).to eq account1.id

end

end


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


describe 'POST /bx_block_notifications/fcm_token_update' do

let(:endpoint) { "/bx_block_notifications/fcm_token_update" }

let(:params) {{ fcm_token: "testing" }}

before { post endpoint, :headers => headers, :params => params }

it 'Update FCM token' do

expect(response.status).to eq 200

expect(json["data"]["id"].to_i).to eq account1.id

end

end


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



Step 6 ::

Now We will change the endpoint -

end point is coming from the top of rspec file


let(:endpoint) { "/bx_block_notifications/triggering_firebase_notification" }






Step 7 :: Process of Rspec Code -


A: Describe -

in describe do give the type of the method -POST and the route – how it is making

describe 'POST /bx_block_notifications/fcm_token_update' do

Type – POST – describe mein jo type liya gaya hai woh route se dekhkar liya gaya hai


B: Set Endpoint -

We set the endpoint – it will go to route - let(:endpoint)

let(:endpoint) { "/bx_block_notifications/fcm_token_update" }


C: Let -

We will send the token inside the parms - we can send any value in the testing -

let(:params) {{ fcm_token: "testing" }}

Here in the parms we will write fcm_token only because fcm_token is inside params in the controller file


D: Before -

before { post endpoint, :headers => headers, :params => params }

before request bhejne ke liye hota hai method par isemein type:post hai headers mein headers diya aur parms mein params diya


E: it -

it : will use this loop - we will use the matchers in this ::

it 'Update FCM token' do

expect(response.status).to eq 200

expect(json["data"]["id"].to_i).to eq account1.id

end


F: Comment :

Will comment this line – we will safely work with above line and at instant this line will not be used thats why .

# expect(json["data"]["id"].to_i).to eq account1.id - Comment it


G:Status of Token:

We will check the status of the token with 200

expect(response.status).to eq 200


H: debugger ::

it 'Update FCM token' do

debugger

expect(response.status).to eq 200

expect(json["data"]["id"].to_i).to eq account1.id

end








I: Check at the Terminal $ ::

Now We will check diff things at the terminal

A. response.status

B. json

C. response.body

D. account1 – in the account first only current user will be.


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


(byebug) response.status

200


(byebug) json

{"data"=>{"id"=>"91", "type"=>"email_account", "attributes"=>{"first_name"=>nil, "last_name"=>nil, "user_name"=>"Test User Notification", "full_phone_number"=>"", "country_code"=>nil, "phone_number"=>nil, "email"=>"email-1@acme.com", "fcm_token"=>"testing", "voip_token"=>nil, "activated"=>false}}}


(byebug) response.body

"{\"data\":{\"id\":\"91\",\"type\":\"email_account\",\"attributes\":{\"first_name\":null,\"last_name\":null,\"user_name\":\"Test User Notification\",\"full_phone_number\":\"\",\"country_code\":null,\"phone_number\":null,\"email\":\"email-1@acme.com\",\"fcm_token\":\"testing\",\"voip_token\":null,\"activated\":false}}}"


(byebug) json

{"data"=>{"id"=>"91", "type"=>"email_account", "attributes"=>{"first_name"=>nil, "last_name"=>nil, "user_name"=>"Test User Notification", "full_phone_number"=>"", "country_code"=>nil, "phone_number"=>nil, "email"=>"email-1@acme.com", "fcm_token"=>"testing", "voip_token"=>nil, "activated"=>false}}}


(byebug) account1

#<AccountBlock::EmailAccount id: 91, first_name: nil, last_name: nil, full_phone_number: "", country_code: nil, phone_number: nil, email: "email-1@acme.com", activated: false, device_id: nil, unique_auth_id: nil, password_digest: [FILTERED], type: "EmailAccount", created_at: "2022-06-17 05:01:28", updated_at: "2022-06-17 05:01:34", last_request_at: nil, user_name: "Test User Notification", login_count: 0, get_email_notifications: true, stripe_id: "cus_LtJxu1a6P4mXLa", fcm_token: nil, voip_token: nil, stripe_plan_id: "plan_LtJxBW1sbq7SBd", stripe_product_id: "prod_LtJxSNqhKjOpMV", stripe_account_id: "acct_1LBXJJQ7dIIpAL3D", device_type: nil, is_deleted: nil, deleted_at: nil, story_display_time: nil, is_onboard_status: false, role: nil, creator_type: nil, badge_icon: nil>


(byebug) account1.id

91


(byebug) json["data"]["id"]

"91"


json["data"]["id"].to_i

91



(byebug) json["data"]["id"].to_i

91


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


Note ::

In account1 mostly current user is used -

Q- Why in the Response only 91 is used --

only 91 is used because in the code current_user is update and we have send the rendor json in the response so in the json at the terminal we have the id of the current_user


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


J. In the Reponse we are getting two outputs to atch ::


it 'Update FCM token' do

expect(response.status).to eq 200

expect(json["data"]["id"].to_i).to eq account1.id

end


expect(response.status).to eq 200 ::

By this we will check the data of OK status 200 that output is OK/Yes in Response or not


expect(json["data"]["id"].to_i).to eq account1.id ::

by this we will check the data of fcm token update that fcm token is updating or not .


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


Now out whole process of output has done with breif description.


Screens ::






Comments

Popular posts from this blog

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