2011-09-01 1 views
7

나는 RESTful API로 빌드되고있는 Rails 3.1 애플리케이션을 가지고있다. 이 계획은 인증 HTTP 헤더를 통해 각 요청에 전달되는 API 키를 기반으로 인증을 처리하는 것입니다. request 객체가 config.before 블록에 존재하지 않기 때문에RSpec.configure와 요청 객체

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 

불행하게도,이 예외가 발생합니다 : RSpec에이를 테스트하기 위해, 나는 config.before 블록의 request.env["HTTP_AUTHORIZATION"] 속성을 설정하고 싶었다.

각 컨트롤러 테스트 파일의 before 블록에이 헤더를 설정하는 또 다른 방법이 있습니까?

답변

2

내가 직접 시도하지 않은하지만 어쩌면 공유 만들기 예 그룹은 문제 분류하는 데 도움이 될 수 :

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