setting up cookies value

31 views
Skip to first unread message

Javix

unread,
May 30, 2014, 5:13:48 AM5/30/14
to rs...@googlegroups.com
I'm coming back to the question regarding how to set up cookies values and read in RSpec controllers spec.
So following the Michael Hartl's Rails Tutorial, I tries to write a controller spec. Michael still uses 'the old' way and put controllers specs in requests folder and requires Capybara in spec_helper:

#config.include Capybara::DSL

I commented the above line as Capybara DSL will be used in features specs and will be loaded without problems.

So, I'd like just to set in some way cookies.permanent and do it in a separate module (spec/support:/login_macros.rb):

module LoginMacros
  def set_admin_session(admin)
    cookies.permanent['remember_token'] = admin.remember_token
  end
end

Then I included the module in spec_helper:

config.include LoginMacros

In my ClientsControllerSpec:

describe ClientsController do
  let(:admin) { create(:admin) }

  before { set_admin_session(admin) }

  describe 'GET #index' do
    it "populates an array of all clients" do
      clients = []
      3.times do
        clients << create(:client)
      end

      get :index
      expect(assigns(:clients)).to match_array(clients)
    end
  end 
end

ClientController:

class ClientsController < ApplicationController
  before_action :signed_in_user

  def index
    @clients = Client.paginate(page: params[:page])
  end
end

Here is 'signed_in_user' method in SessionsHelper:

module SessionsHelper
  def sign_in(user)
    remember_token = Admin.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    user.update_attribute(:remember_token, Admin.digest(remember_token))
    self.current_user = user
  end

  def signed_in_user
    unless signed_in?
      store_location
      redirect_to signin_path, notice: t(:sign_in_required)
    end
  end

end

Here are the errors when running the specs:

ClientsController
  GET #index
    populates an array of all clients (FAILED - 1)
    renders the :index template (FAILED - 2)

Failures:

  1) ClientsController GET #index populates an array of all clients
     Failure/Error: expect(assigns(:clients)).to match_array(clients)
       expected an array, actual collection was nil
     # ./spec/controllers/clients_controller_spec.rb:16:in `block (3 levels) in <top (required)>'

  2) ClientsController GET #index renders the :index template
     Failure/Error: expect(response).to render_template :index
       expecting <"index"> but rendering with <[]>
     # ./spec/controllers/clients_controller_spec.rb:21:in `block (3 levels) in <top (required)>'

What is wrong here ? Why cookies are not set correctly ?
Thank you.




Javix

unread,
May 30, 2014, 5:25:31 AM5/30/14
to rs...@googlegroups.com
Found WHY it did not work.
I had to just sign in the admin user in my LoginMacros and include the corresponding helper (SessionsHelper):

include SessionsHelper

module LoginMacros
  def set_admin_session(admin)
    sign_in(admin)
  end
end

In ClientsControllerSpec :

require 'spec_helper'

describe ClientsController do
  let(:admin) { create(:admin) }

  before { set_admin_session(admin) }

  describe 'GET #index' do
    it "populates an array of all clients" do
      clients = []
      3.times do
        clients << create(:client)
      end

      get :index
      expect(assigns(:clients)).to match_array(clients)
    end

    it "renders the :index template" do
      get :index
      expect(response).to render_template :index
    end
   end

Hope this helps.

Javix

unread,
May 30, 2014, 5:50:34 AM5/30/14
to rs...@googlegroups.com
Even better, correct me if I'm wrong.
No need to add an additional hook (LoginMacros module) because SessionsHelper beign included in my ApplicationController, is automatically accessible in controller specs.
It means that I culd just call SessionsHelper 'sign_in method directly in my ClientControllerSpec:

require 'spec_helper'

describe ClientsController do
  let(:admin) { create(:admin) }

  before { sign_in(admin) }
...
end

and remove the include of LoginMacros module in spec_helper config blolck.

Voilà :)

Javix

unread,
May 30, 2014, 5:56:20 AM5/30/14
to rs...@googlegroups.com
Sorry , it was a little bit wrong.
I had to add an include into spec/support/utilities.rb file:

include SessionsHelper

to make it work.
Reply all
Reply to author
Forward
0 new messages