【问题标题】:I'm learning Rails with M. Hartl's Tutorial. Ch. 11.2.5 and got this error in tests: ArgumentError: bad argument (expected URI object or URI string)我正在通过 M. Hartl 的教程学习 Rails。通道。 11.2.5 并在测试中出现此错误:ArgumentError: bad argument (expected URI object or URI string)
【发布时间】:2013-05-12 11:46:17
【问题描述】:

完整的错误信息:

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 source of code

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

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

【问题讨论】:

    标签: ruby-on-rails ajax rspec


    【解决方案1】:

    在 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

    因此,我将操作 :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)
    

    【讨论】:

    • 这应该是被接受的答案!有完全相同的问题,它的工作原理。谢谢!
    【解决方案2】:

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

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

    你可以使用 capybara 的page.execute_script(但你必须启用 此示例的 javascript :js =&gt; true)

    【讨论】:

    • 对不起,但我想使用与 Rails 教程中完全相同的方法。例如“xhr :post, :create, relationship: { follow_id: other_user.id }”。并且想知道为什么测试是红色的代码行。
    • 这正是我的意思,如果你能提供帮助,你真的不应该使用xhr 方法。去参考它@@api.rubyonrails.org。它在ActionController::TestCase::Behavior 下列出...但您使用的是 RSpec!如果你说使用Test::UnitMiniTest,那就另当别论了。因此建议使用 Capybara 发出 AJAX 请求。 Hartl 不应该鼓励这样做!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    相关资源
    最近更新 更多