Ruby on rails I';与m.Hartl'一起学习Rails;s教程。第11.2.5章,并在测试中得到此错误:ArgumentError:错误参数(预期的URI对象或URI字符串)

Ruby on rails I';与m.Hartl'一起学习Rails;s教程。第11.2.5章,并在测试中得到此错误:ArgumentError:错误参数(预期的URI对象或URI字符串),ruby-on-rails,ajax,rspec,Ruby On Rails,Ajax,Rspec,完整错误消息: 1) RelationshipsController creating a relationship with Ajax should increment the Relationship count Failure/Error: xhr :post, :create, relationship: { followed_id: other_user.id } ArgumentError: bad argument (expected URI object or URI s

完整错误消息:

1) RelationshipsController creating a relationship with Ajax should increment the     Relationship count
Failure/Error: xhr :post, :create, relationship: { followed_id: other_user.id }
ArgumentError: bad argument (expected URI object or URI string)
# ./spec/requests/relationships_controller_spec.rb:14:in `block (4 levels) in <top (required)>'
# ./spec/requests/relationships_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

2) RelationshipsController creating a relationship with Ajax should respond with success
Failure/Error: xhr :post, :create, relationship: { followed_id: other_user.id }
ArgumentError: bad argument (expected URI object or URI string)
# ./spec/requests/relationships_controller_spec.rb:19:in `block (3 levels) in <top (required)>'

3) RelationshipsController destroying a relationship with Ajax should decrement the Relationship count
Failure/Error: xhr :delete, :destroy, id: relationship.id
ArgumentError: bad argument (expected URI object or URI string)
# ./spec/requests/relationships_controller_spec.rb:31:in `block (4 levels) in <top (required)>'
# ./spec/requests/relationships_controller_spec.rb:30:in `block (3 levels) in <top (required)>'

4) RelationshipsController destroying a relationship with Ajax should respond with success
Failure/Error: xhr :delete, :destroy, id: relationship.id
ArgumentError: bad argument (expected URI object or URI string)
# ./spec/requests/relationships_controller_spec.rb:36:in `block (3 levels) in <top (required)>'
1)RelationshipController使用Ajax创建关系时应增加关系计数
失败/错误:xhr:post,:create,关系:{followed\u id:other\u user.id}
ArgumentError:参数错误(应为URI对象或URI字符串)
#./spec/requests/relationships\u controller\u spec.rb:14:in'block(4级)in'
#./spec/requests/relationships\u controller\u spec.rb:13:in'block(3层)in'
2) RelationshipController创建与Ajax的关系时应成功响应
失败/错误:xhr:post,:create,关系:{followed\u id:other\u user.id}
ArgumentError:参数错误(应为URI对象或URI字符串)
#./spec/requests/relationships\u controller\u spec.rb:19:in'block(3层)in'
3) RelationshipController销毁与Ajax的关系时应减少关系计数
失败/错误:xhr:delete,:destroy,id:relationship.id
ArgumentError:参数错误(应为URI对象或URI字符串)
#./spec/requests/relationships\u controller\u spec.rb:31:in'block(4级)in'
#./spec/requests/relationships\u controller\u spec.rb:30:in'block(3层)in'
4) RelationshipController破坏与Ajax的关系时应成功响应
失败/错误:xhr:delete,:destroy,id:relationship.id
ArgumentError:参数错误(应为URI对象或URI字符串)
#./spec/requests/relationships\u controller\u spec.rb:36:in'block(3层)in'

在浏览器中,AJAX工作正常,但测试为红色:(

我对编程、Rails和Stackowerflow相当陌生。 请帮我解决这个问题。:3

看看这是否有帮助:

我还进一步阅读了上面的链接;您应该最大限度地利用Capybara,而不是将rspec与Rails为
操作*::TestCase
提供的方法混合使用

您可以使用capybara的
页面。执行脚本(但必须启用
本例的javascript
:js=>true


在RailsTutorial(Rails 3.2)第11章的相同Ajax部分之后,我遇到了完全相同的问题。我认为MichaelDeSilva是正确的,因为xhr不幸与RSpec混合在一起

无论如何,我决定为了完整性起见(我几乎完成了教程),我打算让xhr在这里工作。我认为Mike Hartl的清单11.37应该使用ActionDispatcher::Integration::RequestHelpers方法xhr,如

而不是ActionController::TestCase::Behavior方法xhr,如

因此,我将对actions:create和:destroy的引用替换为它们的命名路由,清单11.37中的测试示例变为绿色

expect do
  xhr :post, :create, relationship: { followed_id: other_user.id }
end.to change(Relationship, :count).by(1)
expect do
  xhr :delete, :destroy, id: relationship.id
end.to change(Relationship, :count).by(-1)
变成

expect do
  xhr :post, relationships_path, relationship: { followed_id: other_user.id }
end.to change(Relationship, :count).by(1)
expect do
  xhr :delete, relationship_path(relationship.id), id: relationship.id
end.to change(Relationship, :count).by(-1)
还有,原版

expect do
  xhr :post, :create, relationship: { followed_id: other_user.id }
end.to change(Relationship, :count).by(1)
expect do
  xhr :delete, :destroy, id: relationship.id
end.to change(Relationship, :count).by(-1)
变成

expect do
  xhr :post, relationships_path, relationship: { followed_id: other_user.id }
end.to change(Relationship, :count).by(1)
expect do
  xhr :delete, relationship_path(relationship.id), id: relationship.id
end.to change(Relationship, :count).by(-1)

很抱歉,但我希望使用的方法与Rails教程中完全相同。例如“xhr:post,:create,relationship:{followed\u id:other\u user.id}”.想知道为什么测试是红色的是代码行。这正是我的观点,如果你能帮助的话,你真的不应该使用
xhr
方法。去查阅它。它列在
ActionController::TestCase::Behavior
…下。但是你使用的是RSpec!如果你说使用
test::Unit
MiniTest
,然后是另一个故事。因此建议使用水豚来提出AJAX请求。Hartl不应该鼓励这样做!这应该是可以接受的答案!有完全相同的问题,它可以工作。谢谢!