完整的错误信息:
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)>'
My github for源代码
在浏览器中AJAX工作正常,但测试是红色的。(
我对编程、Rails和Stackowerflow都很陌生。请帮我解决这个问题。: 3
我在Rails教程第11章的Ajax部分(Rails 3.2)中遇到了完全相同的问题。我认为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)
看看是否有帮助:RSpec test destroy method (Rails Tutorial 3.2,第9章,第10例)
我还会在上面的链接中进一步阅读;你应该最大限度地利用Capybara,而不是将rspec与Rails为Action*::TestCase
提供的方法混合使用。
你可以使用水豚的
page.execute_script
(但你必须启用):js => true
)