How to add a delay to Rails controller for testing? How to add a delay to Rails controller for testing? ruby ruby

How to add a delay to Rails controller for testing?


Controller like so:

def catalog  #Makes the request pause 1.5 seconds  sleep 1.5  ...end

Even better: only add the sleep for the dev environment.


Elaborating on accepted answer. If you have some base controller like the default ApplicationController which is extended by any other controller you may define the following filter:

class ApplicationController < ActionController::Base  # adds 1s delay only if in development env  before_filter if: "Rails.env.development?" do    sleep 1  endend

Where:1 is number of seconds to wait before returning any response, see sleep docs

This filter will be triggered only if your application is in development environment and it will add desired delay to every request processed by your application.