On Friday, May 30, 2014 11:50:34 AM UTC+2, Javix wrote:
>
>
>
> On Friday, May 30, 2014 11:25:31 AM UTC+2, Javix wrote:
>>
>>
>>
>> On Friday, May 30, 2014 11:13:48 AM UTC+2, Javix wrote:
>>>
>>> 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 
>>> <http://www.railstutorial.org/book>, 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.
>>>
>>> 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.
>>
>
> 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Ă  :)
>

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.

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/6877a1f4-dba3-49b5-aad0-d6d66847df45%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to