Reputation: 1018
I'm running a Rails 5 JSON API server. I have some controller specs written, but I' noticing that they are taking a really long time to run, and I'm looking for a way to optimize.
One way I've read about is to reduce the number of network requests by essentially making the GET/POST requests in a before(:all)
or before(:context)
block, and then having the subsequent it
statements test the response from the single network request.
One issue I'm running into is that within my before(:context)
block, I don't seem to have access to modify the request headers. For example, currently it my code looks like:
before(:each) do
add_authorization_header
get :index
end
where add_authorization_header
looks like:
request.headers['Authorization'] =
"Token token=#{some_authorization_token)}"
but when I chance before(:each)
to before(:context)
I get the following error:
NoMethodError: undefined method `headers' for nil:NilClass
Is there a way to set the request headers within a before(:context)
block?
Upvotes: 1
Views: 278
Reputation: 4640
I think you have no access to the request object inside the example group. Try:
before(:context)
headers = { "Authorization" => "Token token=#{some_authorization_token)}" }
get :index, params: {}, headers: headers
end
Upvotes: 1