我正在使用rspec_api_documentation在 Rails 4 中构建一个 API,这给我留下了深刻的印象。选择使用 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 测试我的 API 的方式。
肯定有人必须这样做吗?还有另一种方法可以存根令牌吗?
提前致谢。