0

完整的错误信息:

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)>'

我的 github 获取代码源

在浏览器 AJAX 工作正常,但测试是红色的。:(

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

4

2 回答 2

2

在 RailsTutorial(使用 Rails 3.2)第 11 章的相同 Ajax 部分之后,我遇到了完全相同的问题。我认为 Michael de Silva 是对的,因为不幸的是 xhr 在这里与 RSpec 混合在一起。

无论如何,为了完整起见,我决定(我几乎完成了教程) - 我打算让 xhr 以某种方式在这里工作。我认为 Mike Hartl 的清单 11.37 应该使用 ActionDispatcher::Integration::RequestHelpers 方法 xhr,如 http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-xhr

而不是 ActionController::TestCase::Behavior 方法 xhr,如 http://api.rubyonrails.org/classes/ActionController/TestCase/Behavior.html#method-i-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 :post, relationships_path, 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 :delete, relationship_path(relationship.id), id: relationship.id
end.to change(Relationship, :count).by(-1)
于 2013-07-30T04:55:17.940 回答
0

看看这是否有帮助: RSpec test destroy method (Rails Tutorial 3.2 Ch. 9, Ex. 10)

我还会在上面的链接中进一步阅读;您应该最大限度地利用 Capybara,而不是将 rspec 与 Rails 为Action*::TestCase. 改为这样做

您可以使用 capybara 的page.execute_script(但您必须为此示例启用 javascript :js => true

于 2013-05-12T12:29:03.160 回答