1
私はレールAPIのアクセプタンス仕様を書いています。リクエスト仕様でこれを行うことができましたが、rspec_api_documentation gemを使用してドキュメントを生成するには、受け入れ仕様を作成する必要があります。acceptance specsのtoken authを使ってレールAPIをテストする適切な方法は何ですか?
私の要求スペックは次のようになります。
let(:headers) { { 'accept': 'application/json' } }
let(:user) { users(:jane) }
describe '#show' do
let(:endpoint) { "/api/v1/users/#{user.username}" }
context 'when authenticated' do
let(:expected_response) { "{\"user\":#{user.to_builder.target!}}" }
let(:headers) { { 'Authorization': token, 'accept': 'application/json' } }
let(:token) do
post '/api/v1/authenticate',
params: { email: user.email, password: 'testpassword' },
headers: { 'accept': 'application/json' }
JSON.parse(response.body)['auth_token']
end
it 'I can get user info' do
get endpoint, params: {}, headers: headers
expect(response.content_type).to eq("application/json")
expect(response.body).to eq expected_response
end
end
end
これまでのところ私はこれを持っていますが、聞かせての変数は、例えば、グループ外にアクセスできないので、それは動作しません。 docs of the gemから
RSpec.resource 'Users' do
header 'Accept', 'application/json'
header 'Content-Type', 'application/json'
get '/api/v1/users/:id' do
explanation 'First, get an auth token, then request user info'
fixtures :users
let(:auth_token) do
client.post '/api/v1/authenticate',
params: { email: user.email, password: 'testpassword' },
headers: { 'accept': 'application/json' }
JSON.parse(response_body)['auth_token']
end
let(:id) { user.id }
let(:user) { users(:jane) }
header 'Authorization', auth_token
example_request 'Getting a specific user' do
expect(response_body).to eq(user.to_json)
expect(status).to eq(200)
end
end
end