2013-10-24 3 views
2

저는 레일즈 4에 rspec_api_documentation을 사용하여 API를 만들고 있습니다. 정말 인상 깊었습니다. DoorKeeper를 사용하여 엔드 포인트를 보호하기로 결정한이 콘솔에서이 모든 것을 성공적으로 테스트 할 수있었습니다.rspec_api_documentation을 사용하여 Doorkeep 토큰 스텁하기

나는 이제 어떻게 문제를 풀어 내는지 명시하고 토큰을 스텁한다.

문지기의 문서는 다음과 같은 사용 제안 :

describe Api::V1::ProfilesController do 
    describe 'GET #index' do 
    let(:token) { stub :accessible? => true } 

    before do 
     controller.stub(:doorkeeper_token) { token } 
    end 

    it 'responds with 200' do 
     get :index, :format => :json 
     response.status.should eq(200) 
    end 
    end 
end 

을 그러나, 나는 rspec_api_documentation에 맞춰 수용 테스트를 작성했습니다. 내가 말했듯이

undefined local variable or method `controller' for #<RSpec::Core 

나는 그것이 명시 적으로 컨트롤러 사양 아니기 때문에이 용의자 :하지만, 내가 테스트를 실행하면

require 'spec_helper' 
require 'rspec_api_documentation/dsl' 

resource "Projects" do 
    header "Accept", "application/json" 
    header "Content-Type", "application/json" 

    let(:token) { stub :accessible? => true } 

    before do 
    controller.stub(:doorkeeper_token) { token } 
    end 

    get "/api/v1/group_runs" do 
    parameter :page, "Current page of projects" 

    example_request "Getting a list of projects" do 
     status.should == 200 
    end 
    end 
end 

내가 다음 얻을 : 이것은 내가 쓴 그 projects_spec.rb입니다 , 내 API를 테스트하는이 rspec_api_documentation 방법을 고집합니다.

확실히 누군가가해야만합니까? 토큰을 스터 빙할 수있는 다른 방법이 있습니까?

미리 감사드립니다.

+0

여기를 보셨습니까? https://relishapp.com/rspec/rspec-rails/v/2-14/docs/controller-specs. 아마'rspec-rails' 보석이 포함되어 있는지 확인하고 예제에': type => : controller' 태그를 붙이시겠습니까? – zetetic

답변

2

rspec_api_documentation 수용 테스트에서 DoorKeeper를 스텁 아웃하지 않는 것이 좋습니다. RAD의 장점 중 하나는 생성되는 예제의 모든 헤더를 보는 것입니다. OAuth2를 스 태킹하면 문서를 읽는 사용자는 클라이언트를 만드는 동안 OAuth2 헤더를 볼 수 없습니다.

잘 할 수 있을지 잘 모르겠습니다. RAD는 Capybara 기능 테스트와 매우 유사하며 빠른 search은 어려운 것처럼 보입니다.

RAD에는 OAuth2MacClient (here)을 사용할 수 있습니다.

require 'spec_helper' 

resource "Projects" do 
    let(:client) { RspecApiDocumentation::OAuth2MACClient.new(self) } 

    get "/api/v1/group_runs" do 
    example_request "Getting a list of projects" do 
     status.should == 200 
    end 
    end 
end 
2

동일한 문제가 발생하여 특정 토큰으로 수동으로 액세스 토큰을 만들었습니다. 그렇게함으로써 Authorization 헤더에서 정의 된 토큰을 사용할 수있었습니다.

resource "Projects" do 
    let(:oauth_app) { 
    Doorkeeper::Application.create!(
     name: "My Application", 
     redirect_uri: "urn:ietf:wg:oauth:2.0:oob" 
    ) 
    } 
    let(:access_token) { Doorkeeper::AccessToken.create!(application: oauth_app) } 
    let(:authorization) { "Bearer #{access_token.token}" } 

    header 'Authorization', :authorization 

    get "/api/v1/group_runs" do 
    example_request "Getting a list of projects" do 
     status.should == 200 
    end 
    end 
end