【问题标题】:assert_select not working after redirect重定向后断言选择不起作用
【发布时间】:2014-09-22 21:20:01
【问题描述】:

assert_select 可以在assert_redirected_to 之后工作吗?我有以下控制器操作,但我遇到了失败。

test "get switchboard" do
  ...
  assert_redirected_to employees_url # success
  assert_select 'div' # fail
end

【问题讨论】:

  • 这不能回答你的问题,但我会尝试在那里只有一个断言。如果redirect_to assert 失败,则不会上报select one ...
  • @Nobita 如 cmets 所示,redirected_to 断言通过。在我添加assert_select之前它也通过了。
  • 我知道,我读过。这只是尝试每次测试只有一个断言的建议。想象一下redirected_to 失败了。你永远不会知道选择一个是否会失败。
  • 我相信将follow_redirect! 放在assert_redirected_to 行之后可能会有所帮助...我想我应该提一下,以防其他人有这个问题。
  • ...assert_redirected_to 跟踪发生的任何重定向...这就是为什么 Rails 的制造商在这个词上加上“ed”结尾...以帮助它更有意义。 api.rubyonrails.org/classes/ActionDispatch/Assertions/…

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 functional-testing


【解决方案1】:

这个问题很老,但我会继续回答它,因为我遇到了类似的问题。 assert_select 可以在重定向后工作,但您必须首先告诉测试“跟随”重定向。所以,在你的情况下,你可以这样做:

test "get switchboard" do
  ...
  assert_redirected_to employees_url # redirected, not "success" per se
  follow_redirect!                   # this is what you need to do
  assert_response :success           # if you still want to...
  assert_select 'div'
end

【讨论】:

    【解决方案2】:

    assert_select 的工作方式与docs 中描述的一样。

    我假设您正在尝试在您要重定向到的页面上的某个元素上执行assert_select。问题在于重定向的路由实际上没有被渲染。

    如果我是你,我要做的第一件事就是在 assert_select 前面扔一个 binding.pry,然后查看 response.body,它会显示实际渲染的内容。我的猜测是你会看到类似的东西:

    <html><body>You are being <a href=\"https://bigoldtest.net/as/1/oauth_provider_credentials?response_code=success\">redirected</a>.</body></html>"
    

    而不是您将被重定向到的实际页面。

    【讨论】:

      猜你喜欢
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 2017-03-20
      • 2016-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      相关资源
      最近更新 更多