0
我想獲取rspec測試中的current_user值,但我沒有得到我正在創建當前用戶,在byebug期間我可以看到current_user,我也可以在admin表中看到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' helper? –
是的,我使用設計和簡單的令牌認證 –
任何人都在這裏幫助我嗎? –