完整错误消息:
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)>'
在浏览器中,AJAX工作正常,但测试是红色的。 :(
我是编程,Rails和Stackowerflow的新手。 请帮我解决这个问题。 :3
答案 0 :(得分:2)
在RailsTutorial(使用Rails 3.2)的第11章的相同Ajax部分之后,我遇到了完全相同的问题。我认为迈克尔德席尔瓦是正确的,因为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
所以我将对操作的引用替换为: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)
答案 1 :(得分:0)
看看这是否有帮助: RSpec test destroy method (Rails Tutorial 3.2 Ch. 9, Ex. 10)
我还会在上面的链接中进一步阅读;你应该最大限度地使用Capybara,而不是将rspec与Rails为Action*::TestCase
提供的方法混合使用。这样做
你可以使用水豚的
page.execute_script
(但你必须启用 这个例子的javascript:js => true
)