我正在使用以下代码从 rake 任务中在服务器上执行请求:
app = ActionDispatch::Integration::Session.new(Rails.application)
app.host!('localhost:3000')
app.get(path)
这很好用。
但是,如果我使用相同的路径再次调用 app.get(path)
,则不会重复请求并返回之前的结果。
有没有办法强制 app.get 重复调用?
最佳答案
尝试重置 session :
app.reset!
这是重置后的工作方式,
def reset!
@https = false
@controller = @request = @response = nil
@_mock_session = nil
@request_count = 0
@url_options = nil
self.host = DEFAULT_HOST
self.remote_addr = "127.0.0.1"
self.accept = "text/xml,application/xml,application/xhtml+xml," +
"text/html;q=0.9,text/plain;q=0.8,image/png," +
"*/*;q=0.5"
unless defined? @named_routes_configured
# the helpers are made protected by default--we make them public for
# easier access during testing and troubleshooting.
@named_routes_configured = true
end
end
否则它只会重新使用最后的响应:
# this is a private method in Session, it will called every time you call `get/post, etc`
def process
......
@request_count += 1
@request = ActionDispatch::Request.new(session.last_request.env)
response = _mock_session.last_response
@response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
@html_document = nil
....
end
祝你好运!
关于ruby-on-rails - 强制 Rails 的 app.get 重做请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659104/