Rails:测试更新操作put请求

时间:2012-11-11 18:36:50

标签: ruby-on-rails-3 rspec request action put

我正在尝试通过更新操作手动更新用户。

我在spec / requests / user_request_spec.rb中尝试了以下代码行:

user = create(:user, username: "username", password: "secret", password_confirmation: "secret") #Factory Girl
attr = { username: "newusername" }
put :update, :id => user.id, :user => attr

我收到以下错误:

Failure/Error: put :update, :id => user.id, :user => attr
ArgumentError:
  bad argument (expected URI object or URI string)
# ./spec/requests/users_request_spec.rb:55:in `block (3 levels) in <top (required)>'

如何通过rspec发送更新用户的请求?

1 个答案:

答案 0 :(得分:0)

我不确定'put'的定义位置,我原本期望没有方法错误。您必须包含定义put的其他内容。

在任何情况下,在测试中加入Rack::Test::Methods,它应该提供您所追求的内容:

describe Foo do
  include Rack::Test::Methods # provides get, post, put, etc

  ...
end