lundi 8 février 2021

expected ActiveRecord::RecordNotFound but nothing was raised

How to get the test pass for this error?

Rspec result

**2) Api::V1::UsersController GET #show - a user it fails showing a user
 Failure/Error:
   expect do
     get 'show', params: { id: 2 }
   end.to raise_error(ActiveRecord::RecordNotFound)
 
   expected ActiveRecord::RecordNotFound but nothing was raised
 # ./spec/controllers/users_controller_spec.rb:100:in `block (3 levels) in <main>'

** Controller -method

  def show
    begin
      user = User.find(params[:id])
      render json: UserSerializer.new(user).serialized_json
    rescue ActiveRecord::RecordNotFound => e
      render json: { error: e.to_s }, status: :not_found      
    end         
  end

**

Rspec controller

it 'it fails showing a user' do
  expect do
    get 'show', params: { id: 2 }
  end.to raise_error(ActiveRecord::RecordNotFound)
end

Aucun commentaire:

Enregistrer un commentaire