breaking into the unknown…

the connection was reset capybara

Leave a comment

While running one of my  spec (loan_documents_controller_spec.rb ) , I’ am getting below error in the capybara driver , with all the spec which make a call to index action getting failed.

the connection was reset
the connection was reset when the page is loading

Thus, capybara show me above error, instead of loading the index page. Since the page is not being loaded, all test I have written for it is getting failed. On closer look to my action, I do find that, I’ am making API call within the action. So when that action is called, The API call get triggered and capybara failed to handle that, thus throwing the error. My controller code is as below.

class LoanDocumentsController < ApplicationController
    def index
       @signed_documents_count = LoanDocument.signed(
       @unsigned_documents_count = LoanDocument.unsigned(

I have defined refresh_docs_from_loan_path in my user model which is making the API call as below

class User < ActiveRecord::Base
     def refresh_docs_from_loan_path
           uri, payload = create_payload
           response = uri, payload, :content_type => “application/xml”  # this is the line making the API call

The solution is either figure out the way for capybara to handle the API call or mock the method which is making the API call. I opted for the second one, infact I do not know how to handle the first one🙂

the single line of code which did the stubing of the refresh_docs_from_loan_path of user is as below :


You, may return whatever you want in place of “anything” which suites your need. Here, the @user is my signed in user for spec which corresponds to the current_user in my controller. The complete sample code with one test is as below.

require ‘spec_helper’

describe LoanDocumentsController do
before(:each) do
@user = FactoryGirl.create(:user)
login_as @user # check it here how to login user in spec controller

    describe “GET ‘index'” do
        it “should load the index page if user logged in” do
             visit loan_documents_path
              page.should have_content(“Document Status”)

We careful to add the mocked line just before capybara visit the action, here I have added just before visit loan_documents_path line. If you do as below

visit root_path
visit loan_documents_path

You will get below error

 Failure/Error: @user.should_receive(:refresh_docs_from_loan_path).and_return(“anything”)
(#<User:0xb6c2c70>).refresh_docs_from_loan_path(any args)
  expected: 1 time
received: 0 times

This is because, the action at root_path do not call the method refresh_docs_from_loan_path which is expected, so you get the error. One thing, I also observed that the mocked line work only with visit not with redirect_to . The below code will also throw the above error

redirect_to loan_documents_path

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s