Session variable not set in test of Rails session controller

457 views Asked by At

I have extended my Devise::SessionsController in order to set a session variable when a user logs in. The session variable is not being set and my test isn't working.

seasons_controller_spec.rb

require 'rails_helper'

describe SessionsController do
  it 'should store true in a session variable if birthdate is set' do
    person = FactoryGirl.create(:person, :birthdate)
    login_as(person)
    session[:birthdate].should eq(true)
  end

  it 'should store false in a session variable if birthdate is not set' do
    login_with create(:person)
    session[:birthdate].should eq(false)
  end
end

sessions_controller.rb

    class SessionsController < Devise::SessionsController
  # POST /resource/sign_in
  def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message!(:notice, :signed_in)
    sign_in(resource_name, resource)
    current_person.birthdate ? session[:birthdate] = true : session[:birthdate] = false
    yield resource if block_given?
    respond_with resource, location: after_sign_in_path_for(resource)
  end

end

Error:

Failures:

  1) SessionsController should store true in a session variable if birthdate is set
     Failure/Error: session[:birthdate].should eq(true)

       expected: true
            got: nil

       (compared using ==)
     # ./spec/controllers/sessions_controller_spec.rb:7:in `block (2 levels) in <top (required)>'
     # /Users/dave/.rvm/gems/ruby-2.3.1/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # -e:1:in `<main>'

  2) SessionsController should store false in a session variable if birthdate is not set
     Failure/Error: session[:birthdate].should eq(false)

       expected: false
            got: nil

       (compared using ==)
     # ./spec/controllers/sessions_controller_spec.rb:12:in `block (2 levels) in <top (required)>'
     # /Users/dave/.rvm/gems/ruby-2.3.1/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # -e:1:in `<main>'

I'm at a loss as to why the variable is not set. Any help would be hugely appreciated. I'm new to testing.

0

There are 0 answers