7

我花了很长时间弄清楚如何使用 Rails 的响应对象登录和注销。标准的博客还可以,但我终于诊断出来了,我想在这里记录一下。

app.get '/'
assert_response :success
app.get '/auth_only_url'
assert_response 302
user = User.find(:user_to_login)
app.post '/signin_url', 
              :user_email => user.email, 
              :user_password => '<password in clear>'
assert_response 302
app.follow_redirect!
assert_response :success
app.get '/auth_only_url'
assert_response :success

请注意,以上暗示您在身份验证请求失败后重定向,并且您在登录后重定向。

为确保将固定装置加载到测试环境数据库中(通常在 rake 测试期间发生),请确保执行以下操作:

 rake db:fixtures:load RAILS_ENV=test

(来自 Patrick Richie)默认 URL 将显示为“www.example.com”,因为此默认主机在 ActionController::Integration::Session 中设置

ActionController::Integration::Session.new.host=> "www.example.com"

它在 actionpack/lib/action_controller/integration.rb#75 中设置

要在集成测试中更改它,请执行以下操作:

session = open_session do |s|  s.host = 'my-example-host.com' end
4

1 回答 1

3

'www.example.com' 是 ActionController::Integration::Session 中设置的默认主机

>> ActionController::Integration::Session.new.host
=> "www.example.com"

它在 actionpack/lib/action_controller/integration.rb#75 中设置

您应该能够通过执行以下操作在集成测试中更改它:

session = open_session do |s|
  s.host = 'my-example-host.com'
end
于 2008-09-05T13:21:07.833 回答