i want to get the current_user value in rspec tests but i am not gettings 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 errrs admin user is not availabl
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: "cordelia@king.biz", 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)
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
Aucun commentaire:
Enregistrer un commentaire