包括'redirect_to'方法在内的测试失败,但app工作正常。迈克尔哈特尔的第9.2.2章(第3.2节)

时间:2012-04-21 18:16:25

标签: ruby-on-rails rspec capybara railstutorial.org

当非登录用户尝试访问用户控制器的编辑或更新操作时,应用应重定向(例如,登录页面或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行)

1 个答案:

答案 0 :(得分: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的新手,甚至没有意识到有日志,但我现在知道了!