ruby-on-rails – 强制Rails的app.get重做请求

我正在使用以下代码在rake任务中在服务器上执行请求:

app = ActionDispatch::Integration::Session.new(Rails.application)
app.host!('localhost:3000')
app.get(path) 

这很好用.

但是,如果我使用相同的路径再次调用app.get(path),则不会重复请求,并返回先前的结果.

有没有办法可以强制app.get重复通话?

最佳答案 尝试重置会话:

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

祝好运!

点赞