西纳特拉 +机架::测试 + Rspec2 - 使用会话?

发布于 2024-12-09 01:58:59 字数 360 浏览 1 评论 0原文

这是我第一次使用 Sinatra,但我无法让会话在我的测试中发挥作用。我在我的应用程序中启用了 :sessions 。

我尝试过:

get "/controller/something", {}, "rack.session" => {:session => "Aa"}

或者

get "/controller/something", {}, "session" => {:session => "Aa"}

但是我的请求中没有设置会话变量。我浏览了网络并尝试了一些建议,但似乎没有任何效果。我错过了什么吗?

谢谢!

It's the first time I'm working with Sinatra and I just can't get sessions to work in my tests. I have enable :sessions in my app.

I tried:

get "/controller/something", {}, "rack.session" => {:session => "Aa"}

or

get "/controller/something", {}, "session" => {:session => "Aa"}

But no session variables are being set in my request. I've looked around the web and tried several suggestions but nothing seems to work. Am I missing something?

Thanks!

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

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

发布评论

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

评论(2

木有鱼丸 2024-12-16 01:58:59

Rack 不再支持通过请求传入会话(Rack >= v1.0)。 阅读这篇文章以获取更多详细信息。

在应用程序中设置会话变量的最佳方法是在应用程序内部调用一个操作来设置会话变量。例如,如果您的应用程序内有一个设置如下会话变量的路由:

post '/set_sess_var/:id'
  session[:user_id] = params[:id]
end

让我们假设您实际上想要测试另一个使用会话变量的路由,如下所示:

get '/get_user_attributes'
  User.find(session[:user_id]).attributes
end

然后在测试中,您应该首先调用该路由它设置会话,然后转到使用它的另一条路由。这是 rspec 表示法,因为这就是我用于测试的方法:

it "should print out user attributes" do
  user_id = 1
  post '/set_sess_var/' + user_id
  get '/get_user_attributes'
  last_response.body.should == User.find(user_id).attributes
end

如果您要在测试中频繁使用该路由,那么您可以在测试文件中编写一个方法来完成此操作(如果您使用 Rspec,则这个方法可以放在你的spec_helper.rb或你的controller_spec.rb文件中):

def set_session_var(user_id)
  post '/set_sess_var/' + user_id
end

然后在你需要设置它时在你的测试中调用它:

it "should print out user attributes" do
  set_session_var(1)
  get '/get_user_attributes'
  last_response.body.should == User.find(1).attributes
end

Rack doesn't support passing in sessions via the request anymore (Rack >= v1.0). Read this post for more detailed information on that.

The best way to set a session variable in your app is to call an action inside of your application that will set the session variable. For instance, if you have a route inside your app that sets a session variable like this:

post '/set_sess_var/:id'
  session[:user_id] = params[:id]
end

Let's pretend there's another route that you actually wanted to test which is using the session variable like this:

get '/get_user_attributes'
  User.find(session[:user_id]).attributes
end

Then in your tests, you should first call the route which sets the session, then go onto another route which uses it. Here is rspec notation, since that is what I use for testing:

it "should print out user attributes" do
  user_id = 1
  post '/set_sess_var/' + user_id
  get '/get_user_attributes'
  last_response.body.should == User.find(user_id).attributes
end

If you were going to be using the route frequently in your tests, then you could write a method to accomplish this in your test file (if you're using Rspec, then this method could go in your spec_helper.rb or in your controller_spec.rb file):

def set_session_var(user_id)
  post '/set_sess_var/' + user_id
end

and then call it in your tests when you needed it to be set:

it "should print out user attributes" do
  set_session_var(1)
  get '/get_user_attributes'
  last_response.body.should == User.find(1).attributes
end
趴在窗边数星星i 2024-12-16 01:58:59

您需要使用最终位于 env 中的密钥:

get "/controller/something", {}, "rack.session" => {:session => "Aa"}

You need to use the keys that will end up in env:

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