我花了很多时间弄清楚如何使用 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
(来自帕特里克·里奇) 默认 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
最佳答案
'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
关于ruby-on-rails - 如何测试从控制台登录Rails?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44619/