0
votes

I want to get the current_user value in rspec tests but I am not getting I am creating the current user and during byebug I can see the the current_user and I can also see the admin_user in admin table but I am getting the errors admin user is not available.

Failures:

1) Camp API Post /camps when the request is valid create a camp Failure/Error: @camp = current_user.camps.create!(camp_params)

 ActiveRecord::InvalidForeignKey:
   PG::ForeignKeyViolation: ERROR:  insert or update on table "camps" violates foreign key constraint "fk_rails_25370d4fd7"
   DETAIL:  Key (admin_id)=(27) is not present in table "admins".
   : INSERT INTO "camps" ("name", "fees", "course", "website", "created_at", "updated_at", "admin_id") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"

and now see the bye bug

Admin id: 27, email: "[email protected]", created_at: "2017-09-23 07:50:32", updated_at: "2017-09-23 07:50:32", authentication_token: "id2a9_K4LfRHJzo6bTNy", first_name: "jamalalll", last_name: "saleemsxd">

now check the code which is creating current_user

def bypass_authentication
  current_user = FactoryGirl.create(:admin)`

  ApplicationController.send(:alias_method, :current_user,:current_user)
  ApplicationController.send(:define_method, :current_user) do
    current_user
  end
  current_user
end

def restore_authentication
  ApplicationController.send(:alias_method, :current_user, :old_current_user)
end

and now i am performing request test for my api

require 'rails_helper'
require 'authentication_helper'

RSpec.describe 'Camp API' , type: :request do
 #initialize the factory
  let!(:camps) {create_list(:camp, 1)}
  let!(:ratings){create_list(:rating, 1)}

  let(:camp_id) {camps.first.id}
  before {bypass_authentication}

  # test  for the GET /camps
  describe 'GET /camps' do

    #make http GET request before  '/camps'
    before {get '/camps'}
    it 'return camps' do
      expect(json).not_to be_empty
      expect(json.size).to eq(1)
    end

    it "returns status code 200" do
      expect(response).to have_http_status (200)
    end
  end

  # test for the GET  /camps/:id
  describe 'GET /camps/:id' do
    before {get "/camps/#{camp_id}"}

    context 'when the record exist' do
        it 'returns to camp' do
            expect(json).not_to be_empty
            expect(json[0]['id']).to eq(camp_id)
      end

      it 'returns status code 200' do
        byebug
        expect(response).to have_http_status(200)
      end
    end
  end

  # test of Post /campa
  describe 'Post /camps' do
    let(:validate_attributes){ {name: "exponent", fees: "300" , course: "Web Development", website: "www.example.com"}}

    context "when the request is valid" do
        before { post '/camps', params: validate_attributes}

        it "create a camp" do
        expect(json['name']).equal?('exponent')

        end

        it "returns the status code 201" do
        expect(response).to have_http_status(201)
        end
    end

    context "when request is not valid" do
      before{ post '/camps', params: {name: 'not camp'}}

      it "return status code 422" do
        expect(response).to  have_http_status(422)
      end

      it "return the validation failure message" do
        expect(response.body).to match(/Validation failed: Fees can't be blank, Website can't be blank, Course can't be blank/)
      end
    end
  end

  # test for put camps/:id
  describe 'PUT /camps/:id' do
    let(:validate_attributes){{nane: 'not camp'}}

    context "when the record exist" do
      before {put "/camps/#{camp_id}", params: validate_attributes}

      it "updates the record" do
        expect(response.body).to be_empty
      end

      it "returns the code 204" do
        expect(response).to  have_http_status(204)

      end
    end
  end

  # test for delete /camp:id
  describe  'DELETE /camps/:id' do
    before {delete "/camps/#{camp_id}"}

    it "returns the code status 204" do
        expect(response).to have_http_status(204)
    end
  end
end
1
How you define current_user? It is devise helper?Зелёный
Yes i am using devise and simple token authenticationArslan shakoor
anyone is here to help me?Arslan shakoor

1 Answers

0
votes

This was actual problem I was facing

 Admin id: 27, email: "[email protected]", created_at: "2017-09-23 07:50:32", updated_at: "2017-09-23 07:50:32", authentication_token: "id2a9_K4LfRHJzo6bTNy", first_name: "jamalalll", last_name: "saleemsxd">

and i found the solution after 30 hours of work and applying different solution.

basically, I was using single table inheritance and providing the foreign key constraint to child model. we cannot use child a foreign key constraint . we need to use parent a foreign key constraint.

class AdminUser < User
  has_many :camps, foreign_key: "user_id" 
end

Here admin user is inherited from User and I want to create a relationship between and adminusers and camps table. but it camps table the foreign key will be user_id not admin_id and schema will look like this

create_table "camps", force: :cascade do |t|
    t.string "name"
    t.integer "fees"
    t.string "course"
    t.integer "graduation_rate"
    t.integer "employment_rate"
    t.integer "averge_salary"
    t.string "website"
    t.string "facebook"
    t.string "twitter"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.bigint "user_id"
    t.index ["user_id"], name: "index_camps_on_user_id"
  end