Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a delay to Rails controller for testing?

I'm testing the front end of a web application and want to test how some of the transitions appear with various delays in between AJAX requests. Is there any way I can add a sleep(1500) to my controller to delay the response?

like image 416
RSG Avatar asked Aug 17 '11 20:08

RSG


2 Answers

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.

like image 183
RSG Avatar answered Nov 16 '22 13:11

RSG


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_action if: "Rails.env.development?" do
    sleep 1
  end
end

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.

like image 19
jmarceli Avatar answered Nov 16 '22 12:11

jmarceli