【问题标题】:Wrong number of arguments error in response响应中的参数错误数量错误
【发布时间】:2015-01-02 21:55:53
【问题描述】:

我正在尝试按照这本书使用 ruby​​ on rails 构建一个 api:http://apionrails.icalialabs.com/book/

但是我在第 5 章中遇到了一个问题,在编写身份验证测试时。

我正在使用 Rails Rails 4.0.2 和 rspec 3.1.7。

测试代码如下:

describe "#authenticate_with_token" do
    before do
      @user = FactoryGirl.create :user
      authentication.stub(:current_user).and_return(nil)
      response.stub(:response_code).and_return(401)
      response.stub(:body).and_return({"errors" => "Not authenticated"}.to_json)
      authentication.stub(:response).and_return(response)
    end

    it "render a json error message" do
      expect(json_response[:errors]).to eql "Not authenticated"
    end

    it {  should respond_with 401 }
  end

(请参见http://apionrails.icalialabs.com/book/chapter_five 上的清单 5.11)

当我运行测试时,我收到以下错误:

 1) Authenticable#authenticate_with_token render a json error message
     Failure/Error: response.stub(:response_code).and_return(401)
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/controllers/concerns/authenticable_spec.rb:28:in `block (3 levels) in <top (required)>'

  2) Authenticable#authenticate_with_token 
     Failure/Error: response.stub(:response_code).and_return(401)
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/controllers/concerns/authenticable_spec.rb:28:in `block (3 levels) in <top (required)>'

我还尝试在不使用存根的情况下编写代码,如下所示:

allow(authentication).to receive(:current_user).and_return(nil)
allow(response).to receive(:response_code).and_return(401)
allow(response).to receive(:body).and_return({"errors" => "Not authenticated"}.to_json)
allow(authentication).to receive(:response).and_return(response)

但我仍然收到错误的数字参数错误:

Failures:

  1) Authenticable#authenticate_with_token render a json error message
     Failure/Error: allow(response).to receive(:response_code).and_return(401)
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/controllers/concerns/authenticable_spec.rb:33:in `block (3 levels) in <top (required)>'

  2) Authenticable#authenticate_with_token 
     Failure/Error: allow(response).to receive(:response_code).and_return(401)
     ArgumentError:
       wrong number of arguments (2 for 1)
     # ./spec/controllers/concerns/authenticable_spec.rb:33:in `block (3 levels) in <top (required)>'

如果你能帮助我,那就太好了,

谢谢!

【问题讨论】:

  • 你使用的是什么版本的 Rails 和 RSpec?
  • 这可能是 rspec 的版本问题,请确保将 rspec 的版本锁定为 2.14,如下所示:gem "rspec-rails", "~&gt; 2.14" 您可能必须删除Gemfile.lock 文件,然后运行捆绑安装命令
  • 嗨 @dnunez24 我正在使用 Rails Rails 4.0.2 和 rspec 3.1.7。
  • 如果您确定发生故障的行(即authenticable_spec.rb 的第 33 行)会有所帮助。

标签: ruby-on-rails ruby api rspec


【解决方案1】:

改成

allow(response).to receive(:status).and_return(401) it { expect(response.status).to eq(401) }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-24
    相关资源
    最近更新 更多