Ruby on rails 参数数目错误响应中出现错误
我正试图通过以下这本书使用ruby on rails构建一个api: 然而,在第5章中,我在编写身份验证测试时遇到了一个问题 我使用的是Rails 4.0.2和rspec 3.1.7 测试代码如下所示:Ruby on rails 参数数目错误响应中出现错误,ruby-on-rails,ruby,api,rspec,Ruby On Rails,Ruby,Api,Rspec,我正试图通过以下这本书使用ruby on rails构建一个api: 然而,在第5章中,我在编写身份验证测试时遇到了一个问题 我使用的是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)
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
(请参见上的清单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)>'
但我仍然得到了错误的参数错误:
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)>'
故障:
1) 可验证#使用_令牌验证_呈现json错误消息
失败/错误:允许(响应)。接收(:响应\代码)。和\返回(401)
参数错误:
参数数量错误(2对1)
#./spec/controllers/concerns/authenticable_spec.rb:33:in'block(3级)in'
2) 可验证的#使用_令牌验证
失败/错误:允许(响应)。接收(:响应\代码)。和\返回(401)
参数错误:
参数数量错误(2对1)
#./spec/controllers/concerns/authenticable_spec.rb:33:in'block(3级)in'
如果你能帮助我那就太好了
谢谢 更改为
allow(response).接收(:status).并返回(401)
它{expect(response.status).to eq(401)}
您使用的是什么版本的Rails和RSpec?这可能是RSpec的版本问题,请确保将RSpec的版本锁定为2.14,如下所示:gem“RSpec Rails”,“~>2.14”
您可能必须删除Gemfile.lock
文件,然后运行bundle安装commandHi@dnunez24。我使用的是Rails Rails 4.0.2和rspec 3.1.7。如果您能够识别故障发生的行(即Authenticatable_spec.rb
的第33行),这会有所帮助。
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)>'