Ruby on rails 使用rspec_api_文档剔除门卫令牌
我正在使用Rails 4构建一个API,这给我留下了深刻的印象。选择使用DoorKeeper保护端点后,我成功地从控制台测试了这一切,并使其正常工作 我现在有困难的地方是如何规范它,并存根令牌 门卫的文件建议使用以下内容:Ruby on rails 使用rspec_api_文档剔除门卫令牌,ruby-on-rails,rspec,oauth-2.0,doorkeeper,Ruby On Rails,Rspec,Oauth 2.0,Doorkeeper,我正在使用Rails 4构建一个API,这给我留下了深刻的印象。选择使用DoorKeeper保护端点后,我成功地从控制台测试了这一切,并使其正常工作 我现在有困难的地方是如何规范它,并存根令牌 门卫的文件建议使用以下内容: describe Api::V1::ProfilesController do describe 'GET #index' do let(:token) { stub :accessible? => true } before do c
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
undefined local variable or method `controller' for #<RSpec::Core
然而,我已经根据rspec_api_文档编写了一个验收测试。这是我写的projects\u spec.rb
:
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
当我运行测试时,我得到以下信息:
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
undefined local variable or method `controller' for #<RSpec::Core
未定义的局部变量或#的方法“controller”,我不建议在rspec#U api#U文档验收测试中取消门卫。RAD的好处之一是可以在它生成的示例中看到所有的头。如果您要删除OAuth2,那么阅读文档的人在尝试创建客户机时将看不到任何OAuth2头
我也不确定是否有可能做到这一点。RAD与水豚特征测试非常相似,快速测试似乎很难完成
RAD有一个OAuth2Acclient
,您可以使用它
我也遇到了同样的问题,我用指定的令牌手动创建了访问令牌。这样,我就可以在授权标头中使用我定义的令牌:
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
你看过这里吗。可能会检查是否包含rspec rails
gem,并用:type=>:controller
标记示例?