无法在RSPEC中设置会话

发布于 2025-01-31 04:06:41 字数 800 浏览 1 评论 0原文

这是我要在此处测试的文件

module Cesid
  module Access
    def cesid_signout
      return unless session_logged
      
      # Method to modify the cookie 
      set_cookie
    end

    def session_logged
      session[:cesid_logged]
    end
  end
end

是我的RSPEC文件,

describe Cesid::Access do
  context '#cesid_signout' do
    let!(:access) { Class.new { extend Cesid::Access } }
    it 'does nothing if not cesid logged' do
      session[:cesid_logged] = false
      expect(access.session_logged).to eql(false)
    end
  end
end

我在运行RSPEC后只是遇到错误。日志只是继续打印它,我无法查看错误的第一行

# /Users/renee.sarmiento/.rvm/gems/ruby-2.5.1@ls-member/gems/actionpack-4.2.8/lib/action_dispatch/testing/test_process.rb:14:in `session'

?谢谢你!

Here is the file I am trying to test

module Cesid
  module Access
    def cesid_signout
      return unless session_logged
      
      # Method to modify the cookie 
      set_cookie
    end

    def session_logged
      session[:cesid_logged]
    end
  end
end

Here is my rspec file

describe Cesid::Access do
  context '#cesid_signout' do
    let!(:access) { Class.new { extend Cesid::Access } }
    it 'does nothing if not cesid logged' do
      session[:cesid_logged] = false
      expect(access.session_logged).to eql(false)
    end
  end
end

I'm just getting an error after running the rspec. The logs just keep printing this and I can't view this first line of the error

# /Users/renee.sarmiento/.rvm/gems/ruby-2.5.1@ls-member/gems/actionpack-4.2.8/lib/action_dispatch/testing/test_process.rb:14:in `session'

Any ideas? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡写薰衣草的香 2025-02-07 04:06:41

在您的用例中,我没有所有上下文。

但是很明显,您无法访问session在缓存的变量访问中,因为session不是实例变量。

因此,我建议两件事:

  • 首先,请确保您可以在模块中启动session实例变量如下所示
module Cesid
  module Access
    def cesid_signout
      return unless session_logged

      # Method to modify the cookie
      set_cookie
    end

    def session
      @session ||= {}
    end

    def session_logged
      @session[:cesid_logged]
    end
  end
end
  • 然后,在您的规格中,不仅要实例化会话,而且要以正确的方式访问:
context '#cesid_signout' do
  let!(:access) { Class.new { extend Cesid::Access } }

  it 'does nothing if not cesid logged' do
    access.session[:cesid_logged] = false
    expect(access.session_logged).to eql(false)
  end
end

通过这些修改,您的RSPEC测试应通过。

当然,这是解决问题的一种方法,但是根据您的用例,其他解决方案可能会起作用。

希望它能帮助您的理解。
对模块的工作方式有更多的了解在这里

I don't have all the context in your use case.

But it is clear that you don't have access to session into your cached variable access since session is not an instance variable.

So I suggest two things:

  • First, make sure that you can initiate your session instance variable into your module as explain here
module Cesid
  module Access
    def cesid_signout
      return unless session_logged

      # Method to modify the cookie
      set_cookie
    end

    def session
      @session ||= {}
    end

    def session_logged
      @session[:cesid_logged]
    end
  end
end
  • Then, in your spec, make sure not only to instantiate your session but also access it the right way :
context '#cesid_signout' do
  let!(:access) { Class.new { extend Cesid::Access } }

  it 'does nothing if not cesid logged' do
    access.session[:cesid_logged] = false
    expect(access.session_logged).to eql(false)
  end
end

With those modification, your RSpec tests should pass.

Of course it is one way to solve your problem, but depending on your use case, other solution might work.

Hope it'll help your understanding.
More insight of how module works here just in case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文