Env [ENV_SESSION_OPTIONS_KEY]的ActionDispatch nil值

时间:2015-06-08 16:53:37

标签: ruby-on-rails-4 rspec session-cookies

我试图运行一个capybara cookie功能规范,当我调用visit [URL]方法时,堆栈跟踪会给我以下错误。

Failures:

  1) Cookie Configuration writes cookies
     Failure/Error: visit('/cookies')
     NoMethodError:
       undefined method `[]=' for nil:NilClass
     # /Users/amat/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/actionpack-4.0.13/lib/action_dispatch/request/session.rb:169:in `load!'
     # /Users/amat/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/actionpack-4.0.13/lib/action_dispatch/request/session.rb:164:in `load_for_write!'
     # /Users/amat/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/actionpack-4.0.13/lib/action_dispatch/request/session.rb:153:in `merge!'
     # /Users/amat/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/actionpack-4.0.13/lib/action_dispatch/request/session.rb:13:in `create'
     # /Users/amat/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/actionpack-4.0.13/lib/action_dispatch/middleware/session/abstract_store.rb:69:in `prepare_session'
     # /Users/amat/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.5.3/lib/rack/session/abstract/id.rb:224:in `context'
     # /Users/amat/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/rack-1.5.3/lib/rack/session/abstract/id.rb:220:in `call'
     # /Users/amat/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/actionpack-4.0.13/lib/action_dispatch/middleware/cookies.rb:486:in `call'

我怀疑的部分问题是actionpack-4.0.13/lib/action_dispatch/request/session.rb我们有代码:

def self.find(env)
  env[ENV_SESSION_OPTIONS_KEY]
end

返回nil,因为我认为env [ENV_SESSION_OPTIONS_KEY]没有设置。并且因为它在调用时返回nil

def load!
   id, session = @by.load_session @env
   options[:id] = id
   @delegate.replace(stringify_keys(session))
   @loaded = true
end

options[:id]给出了上面的堆栈跟踪中看到的错误。

这是我手动在测试中必须存根的东西吗?我可能缺少一种配置吗?

编辑:如果你想查看规范代码,那就相当简单了:

require 'spec_helper'

describe 'Cookie Configuration', type: :feature do
  it 'writes cookies' do
    visit('/cookies')
    ..
  end
end

0 个答案:

没有答案