1
共有コンテキストを使用してRSpec要求仕様をDRYしようとしています。私はlet
の変数を共有コンテキスト間で共有して、互いに継承して拡張したいと考えています。予想通りtoken
作品を宣言するが、NoMethodError
示唆super()
がnil headers
リターンを上書きするsuper()
を使用してRSpec変数を共有コンテキスト間で共有する方法は?
Rspec.shared_context 'JSON request' do
let(:headers) do
{
'Accept' => 'application/json'
}
end
end
Rspec.shared_context 'Authenticated request' do
let(:headers) do
super().merge('Authorization' => "Bearer #{token}")
end
end
Rspec.describe 'user management' do
let(:token) { create(:oauth_token) }
include_context 'JSON request'
include_context 'Authenticated request'
it 'responds with a 200 ok' do
get '/user', headers: headers
expect(response).to have_http_status(:ok)
end
end
。
完璧、ありがとう! –