在Rails会话控制器的测试中未设置会话变量

时间:2016-12-15 06:06:01

标签: ruby-on-rails rspec devise guard

我已经扩展了我的Devise :: SessionsController,以便在用户登录时设置会话变量。会话变量未设置且我的测试不起作用。

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

错误:

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>'

我不知道为什么没有设置变量。任何帮助将非常感激。我很擅长测试。

0 个答案:

没有答案