我正在使用rspec_api_documentation在Rails4中构建一个应用程序接口,这给我留下了深刻的印象。在选择使用DoorKeeper来保护我的端点之后,我能够成功地从控制台测试这一切,并使其正常工作。
我现在遇到的困难是如何指定它,并将令牌存根。
DoorKeeper的文档建议使用以下内容:
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的验收测试。这是我写的projects_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当我运行测试时,我得到以下结果:
undefined local variable or method `controller' for #<RSpec::Core我怀疑这是因为它不是明确的控制器规范,但正如我所说的,我宁愿坚持使用这种rspec_api_documentation方法来测试我的应用程序接口。
肯定有人不得不这么做吧?有没有其他方法可以将令牌存根?
提前谢谢。
发布于 2014-08-14 09:16:05
我也遇到了同样的问题,我用指定的令牌手动创建了访问令牌。这样,我就可以在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发布于 2013-10-25 20:27:42
我不建议在rspec_api_documentation验收测试中清除DoorKeeper。RAD的一个好处是可以在它生成的示例中看到所有的头文件。如果您正在清除OAuth2,那么阅读文档的人在尝试创建客户端时将看不到任何OAuth2头文件。
我也不确定是否有可能很好地做到这一点。RAD非常类似于卡皮巴拉的功能测试,快速的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
endhttps://stackoverflow.com/questions/19577787
复制相似问题