2

当未登录用户尝试访问用户控制器的编辑或更新操作时,应用程序应重定向(例如,登录页面或 root_path)。应用程序工作正常,但对此的测试失败并出现以下错误:

Failure/Error: specify { response.should redirect_to(signin_path) }
Expected response to be a redirect to <http://www.example.com/signin> but was a redirect to <https://www.example.com/users/468>
# ./spec/requests/authentication_pages_spec.rb:69:in `block (6 levels) in <top (required)>'

这里可能有什么问题?如何调试它?

所有代码都在这里:https ://github.com/tomek-rusilko/miniatury_katalog_2 失败的测试在这里:https ://github.com/tomek-rusilko/miniatury_katalog_2/blob/master/spec/requests/authentication_pages_spec.rb (行:63 和 105)

4

1 回答 1

2

终于有时间重温这个了。我从 github 中提取了您的代码并遇到了同样的问题。我将测试缩减为:

require 'spec_helper'

describe "A non-signed_in user directly issuing PUT to the update action" do
  before { put user_path(7) }
  specify { response.should redirect_to(signin_path) }          
end

正如您所指出的,控制器代码中的 binding.pry 并没有像我想象的那样停止执行。经过大量无用的挖掘/谷歌搜索,终于在 log/test.log 中注意到:

Started PUT "/users/7" for 127.0.0.1 at 2012-05-23 16:17:31 -0400
Processing by UsersController#update as HTML
  Parameters: {"id"=>"7"}
Redirected to https://www.example.com/users/7
Filter chain halted as #<Proc:0x00000102201608@/Users/eric_mccullough/ruby/railstutorial/tomek-rusilko-miniatury_katalog_2-e73d795/vendor/ruby/1.9.1/gems/actionpack-3.2.3/lib/action_controller/metal/force_ssl.rb:28> rendered or redirected
Completed 301 Moved Permanently in 1ms (ActiveRecord: 0.0ms)
   (0.1ms)  rollback transaction
   (0.1ms)  begin transaction

它试图使用 SSL。我的 Rails 教程版本仅在生产中使用 SSL。我从 application_controller.rb 中删除了“force_ssl”,然后所有测试都通过了。我还是 Rails 的新手,甚至没有意识到有日志,但我现在知道了!

于 2012-04-25T15:50:28.773 回答