rspecテストでcurrent_userの値を取得したいのですが、私は現在のユーザーを作成していません。また、byebugの間にcurrent_userが表示され、admin_userもadminテーブルに表示されますが、利用できません。rspec要求テスト中にcurrent_userが値を取得しませんか?
障害:
1)キャンプAPIポスト/キャンプ要求がキャンプ 失敗/エラー有効作成されています!@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"
と今さようならバグ
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">
は今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
、今私は私の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
current_userの定義方法は?それは '工夫 'ヘルパーですか? –
はい私はdeviseと単純なトークン認証を使用しています –
誰も私を助けるためにここにありますか? –