lundi 18 juin 2018

This is my user controller. How to test this code in RSpec?

How to write test case in this user log in. i am newbie in rails testing i am using Rspec gem. i need any tutorial for rails test.

if params.key?(:mobile) && params.key?(:password)
  user = User.find_by(mobile: params[:mobile], is_active: 1)
  if user.nil?
    output = { status: '06', message: 'Invalid user.' }.to_json
  elsif user.authenticate(params[:password])
    auth_token = JsonWebToken.encode(id: user.id,
                                     user_type: user.user_type)
    object = { id: user.id, user_type: user.user_type,
               name: user.name }
    output = { status: '10', message: 'Successful.',
               auth_token: auth_token, user: object}.to_json
  else
    output = { status: '06', message: 'Incorrect password.' }.to_json                         
  end
else
  output = { status: '01', message: 'Invalid parameters.' }.to_json
end

render json: output, status: :ok

Aucun commentaire:

Enregistrer un commentaire