我正在为rails api编写验收规范。我能够在请求规范中执行此操作,但我想构建验收规范,以便使用rspec_api_documentation gem生成文档。
我的请求规范如下:
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
到目前为止,我有这个但它不起作用,因为在示例组之外不能访问变量。
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
答案 0 :(得分:1)
标题
此方法采用标题名称和值。值可以是字符串或符号。如果它是符号,它将发送符号,允许您设置标题值。
基于此,我认为您必须声明这样的标题:
header 'Authorization', :auth_token
查看引用段落下方文档中的示例。