assert_select在重定向后无法正常工作

时间:2014-09-22 21:20:01

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

assert_select之后assert_redirected_to能否正常工作?我有以下控制器操作,我遇到了失败。

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

2 个答案:

答案 0 :(得分:2)

这个问题很老了,但是我会继续回答它,因为我遇到了类似的问题。 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

答案 1 :(得分:0)

assert_select的工作原理与docs中描述的相同。

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

如果我是你,我会做的第一件事就是在binding.pry前面放assert_select并看看response.body,这将揭示实际上是什么渲染。我的猜测是你会看到类似的东西:

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

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

相关问题