【问题标题】:Rspec request.env wrong number of argumentsRspec request.env 参数数量错误
【发布时间】:2014-02-10 19:14:44
【问题描述】:

这是我的代码:

before(:each) do
  request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials(api_key.access_token)
end

我的测试应该通过,但它显示此错误:

Failure/Error: request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Token.encode_credentials("#{api_key.access_token}")
 ArgumentError:
   wrong number of arguments (0 for 1..2) 
  • 宝石:

    rspec-rails 版本 = 2.14.1。

    rails 版本 = 4

    来自'git://github.com/mongoid/mongoid.git'的mongoid

【问题讨论】:

  • api_key 设置在哪里?
  • 调用 ApiKey.create 有效吗?在每个块之前尝试 api_key = ApiKey.create ,看看这是否是引发错误的原因
  • 你能在你的 Rails 控制台试试这个吗? ActionController::HttpAuthentication::Token.encode_credentials("anything")。它会抛出错误吗?
  • 不确定发生了什么。通过使用 --backtrace 选项运行 rspec 来发布完整的错误消息
  • 我会在您收到错误的那一行打印出method(:request)request.method(:env),以确保您正在调用您认为的request 方法。

标签: ruby-on-rails rspec2 rspec-rails


【解决方案1】:

您在测试中访问的 request 方法记录在 http://rubydoc.info/gems/rack-test/0.6.2/Rack/Test/Session:request 中,并且需要 1 或 2 个参数,如错误所示。您在 request.env 上遇到错误,因为您在没有参数的情况下调用 request

【讨论】:

  • 稍后实现。
  • @FelippeDaMottaRaposo 我很好奇,你最终是如何实现这个的?
  • 对不起,我真的不记得了。我在 3 年前停止在这个项目中工作! ://
猜你喜欢
  • 2018-09-22
  • 1970-01-01
  • 2013-06-22
  • 2015-02-07
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
相关资源
最近更新 更多