响应中错误的参数数量错误

时间:2015-01-02 21:55:54

标签: ruby-on-rails ruby api rspec

我正在尝试按照本书在轨道上建立一个带有红宝石的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)>'

如果你可以帮助我,那将是很好的,

谢谢!

0 个答案:

没有答案