RSpec.configure and request object

I have a Rails 3.1 application that is created as a RESTful API. The plan is to handle authentication based on the API key, which is transmitted for each request through an HTTP authorization header. To test this in RSpec, I wanted to set the request.env["HTTP_AUTHORIZATION"] attribute in the config.before block:

 RSpec.configure do |config| config.mock_with :rspec config.use_transactional_fixtures = true config.before(:each) do # Set API key in Authorization header request.env["HTTP_AUTHORIZATION"] = "6db13cc8-815f-42ce-aa9e-446556fe3d72" end end 

Unfortunately, this throws an exception because the request object does not exist in the config.before block.

Is there any other approach to setting this header outside of inclusion in the before block of each controller test file?

+7
source share
1 answer

I have not tried it myself, but perhaps creating a group of common examples could help you sort out your problem:

  shared_examples_for "All API Controllers" do before(:each) do request.env["HTTP_AUTHORIZATION"] = "blah" end # ... also here you can test common functionality of all your api controllers # like reaction on invalid authorization header or absence of header end describe OneOfAPIControllers do it_should_behave_like "All API Controllers" it "should do stuff" do ... end end 
+2
source

All Articles