我假设ApiKey是一个ActiveRecord模型,对吗?curl命令针对开发数据库运行,测试针对测试数据库运行。我在你的片段中看不到任何设置ApiKey的内容。除非你在其他地方有,否则试着按照以下方式添加一些内容:
it "passes the token" do
# use factory or just create record with AR:
ApiKey.create!(:access_token => 'test_access1', ... rest of required attributes ...)
# this part remains unchanged
get :new, nil,
:authorization => ActionController::HttpAuthentication::Token.encode_credentials("test_access1")
assigns(:token).should be "test_access1"
end
您可以稍后将其移动到
before :each
块或支撑模块。
更新:
看到你的评论后,我不得不深入研究。这是另一个猜测。这种形式的
get
get '/path', nil, :authorization => 'string'
应该只在集成测试中工作。对于控制器测试,身份验证准备应该如下所示:
it "passes the token" do
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials("test_access1")
get :new
assigns(:token).should be "test_access1"
end
这背后的原因来自各个测试模块的方法签名:
# for action_controller/test_case.rb
def get(action, parameters = nil, session = nil, flash = nil)
# for action_dispatch/testing/integration.rb
def get(path, parameters = nil, headers = nil)