How to test JSON result from RoR functional tests

If your Rails controllers return JSON code, you have to call ActiveSupport::JSON.decode on the response body to get a hash to use in your functional tests.
First of all, add the code below on test/test_helper.rb file inside the ActionController::TestCase class:

# Added to support JSON test
def json
    ActiveSupport::JSON.decode @response.body

So, next step is make the JSON test. If you have an index method as below:

def index
    respond_to do |format|
        format.json { render json: }

The test below covers the HTML part:

test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:user)

To cover also the JSON code, just add the following:

test "should get JSON index " do
    get :index , :format => :json
    assert_response :success
    assert_not_nil assigns(:user).to_json, @response.body

Check the doc to go in deep about encode e decode methods here,

RoR: How to test a confirm dialog with Capybara?

Capybara is a great Gem about integration tests in Ruby on Rail. It simulates how a real user would interact with your application by the browser.
This post explains how to face the javascript confirmation pop up windows (“Are you sure?”).
Seems like there is no way to handle it in Capybara but, If you’re using a drivers that supports JavaScript as Selenium, the best solution is override the confirm method to always return true.

page.evaluate_script('window.confirm = function() { return true; }')'Destroy')

Learn VIM


A great resources collection about VIM (expecially for Rails developer) by thoughtbot

“Unfortunately” some resources are not free, but it’s worth going to visit the website in any case.

Learn VIM

About me

Giuseppe La Rosa

IT Specialist

Milan, Italy

Follow me on:


Hi, my name is Giuseppe La Rosa, I’m a System Engineer living in Milan (Italy). I was born in Catania (Italy) in 1978. I started to get interested in the world of computer science at the age of 8 years. I graduated in 2006 with a degree in Information Technology and then I attended a stage at New York University for 3 months in bioinformatics algorithms filed.
After that, I came back to Italy and I worked in several roles as Java Developer and System administrator. Now I am an Operations Business Manager in a big Telco company.
I have a deep passions in several fields related to IT as OS, languages, security, Cloud and much more.


OS: Linux, Unix, HP-UX, Oracle Exadata
Languages : Java (J2SE, J2EE, Servlet), Python, Ruby on Rails, Puppet, Shell scripting
Web languages: JSP, ASP, HTML, CSS, XML+DTD
Tools & other: HP Service Guard, Veritas Cluster, Veritas Volume Manager


RHCA (Red Hat Certified Architect)
Red Hat Certified System Administrator in OpenStack
Platform-as-a-Service: Red Hat OpenShift Enterprise
Red Hat Server Hardening
Red Hat Performance Tuning
RHCVA (Red Hat Certified Virtualization Administrator)
RHCE (Red Hat Certified Engineer)
ITIL V3 Foundation
Ec-Council Certified Ethical Hacker v7


Photo, Ju Jitsu and above all motorbike