Wrong number of arguments 响应错误

Wrong number of arguments error in response

我正在尝试按照以下这本书在 rails 上用 ruby 构建一个 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)>'

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

谢谢!

改为

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